Types
This chapter describes some types identification and concepts setup defined by the module MAD.typeid
and MAD._C
(C API). The module typeid
is extended by types from other modules on load like e.g. is_range, is_complex, is_matrix, is_tpsa, etc…
Typeids
All the functions listed hereafter return only true
or false
when identifying types.
Primitive Types
The following table shows the functions for identifying the primitive type of LuaJIT, i.e. using type(a) == 'the_type'
Functions |
Return |
---|---|
|
is a nil |
|
is a boolean |
|
is a number |
|
is a string |
|
is a function |
|
is a table |
|
is a userdata |
|
is a thread [1] |
|
is a cdata |
Extended Types
The following table shows the functions for identifying the extended types, which are primitive types with some extensions, specializations or value ranges.
Functions |
Return |
---|---|
|
is |
|
is |
|
is |
|
is a boolean or |
|
is a number with \(|a| < \infty\) |
|
is a number with \(|a| = \infty\) |
|
is a number with \(a > 0\) |
|
is a number with \(a < 0\) |
|
is a number with \(a \ge 0\) |
|
is a number with \(a \le 0\) |
|
is a number with \(a \ne 0\) |
|
is a number with \(-2^{52} \le a \le 2^{52}\) and no fractional part |
|
is a number with \(-2^{31} \le a < 2^{31}\) and no fractional part |
|
is an integer with \(a \ge 0\) |
|
is an integer with \(a > 0\) |
|
is an even integer |
|
is an odd integer |
|
is not an integer |
|
is a string with |
|
is a string with valid identifier characters, i.e. |
|
is a table with no metatable |
|
is a table with no element |
|
is a userdata with |
|
is a userdata with |
|
is a userdata with |
|
is an open file with some content |
Concepts
Concepts are an extention of types looking at their behavior. The concepts are more based on supported metamethods (or methods) than on the types themself and their valid range of values.
Functions |
Return |
---|---|
|
is a nil, a boolean, a number or a string |
|
is not a value |
|
is a mappable and 1st iteration returns |
|
supports operation |
|
supports operation |
|
supports operation |
|
supports operation |
|
supports operation |
|
supports operation |
|
supports operation |
|
supports operation |
|
supports operation |
|
supports operation |
|
supports operation |
|
supports operation |
|
supports operation |
|
supports operation |
|
supports operation |
|
supports operation |
|
supports metamethod |
|
supports metamethod |
|
supports metamethod |
|
supports metamethod |
|
defines metamethod |
|
has metamethods for restriction, see |
|
has metamethods for protection, see |
|
has metamethods for deferred expressions, see |
|
has the same type and metatable as |
The functions in the following table are complementary to concepts and usually used to prevent an error during concepts checks.
Functions |
Return |
---|---|
|
|
|
|
|
metamethod |
|
|
- is_metaname(a)
Returns
true
if the stringa
is a valid metamethod name,false
otherwise.
- get_metatable(a)
Returns the metatable of
a
even ifa
is a cdata, which is not the case ofgetmetatable()
.
- get_metamethod(a, f)
Returns the metamethod (or method)
f
ofa
even ifa
is a cdata andf
is only reachable through the metatable, ornil
.
Setting Concepts
- typeid.concept
The table
concept
contains the lists of concepts that can be passed to the functionset_concept
to prevent the use of their associated metamethods. The concepts can be combined together by adding them, e.g.not_comparable = not_equalable + not_orderable
.
Concepts |
Associated metamethods |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- set_concept(mt, concepts, strict_)
Return the metatable
mt
after setting the metamethods associated to the combination of concepts set inconcepts
to prevent their use. The concepts can be combined together by adding them, e.g.not_comparable = not_equalable + not_orderable
. Metamethods can be overridden ifstrict = false
, otherwise the overload is silently discarded. Ifconcepts
requires iterable but not mappable thenpairs
is equivalent toipairs
.
- wrestrict(a)
Return a proxy for
a
which behaves likea
, except that it prevents existing indexes from being modified while allowing new ones to be created, i.e.a
is extendable.
- wprotect(a)
Return a proxy for
a
which behaves likea
, except that it prevents existing indexes from being modified and does not allow new ones to be created, i.e.a
is readonly.
- wunprotect(a)
Return
a
from the proxy, i.e. expect a restricted or a protecteda
.
- deferred(a)
Return a proxy for
a
which behaves likea
except that elements of type function will be considered as deferred expressions and evaluated on read, i.e. returning their results in their stead.
C Type Sizes
The following table lists the constants holding the size of the C types used by common cdata like complex, matrices or TPSA. See section on C API for the description for those C types.
C types sizes |
C types |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
C API
-
type log_t
The logical type aliasing _Bool, i.e. boolean, that holds
TRUE
orFALSE
.
-
type idx_t
The index type aliasing int32_t, i.e. signed 32-bit integer, that holds signed indexes in the range \([-2^{31}, 2^{31}-1]\).
-
type ssz_t
The size type aliasing int32_t, i.e. signed 32-bit integer, that holds signed sizes in the range \([-2^{31}, 2^{31}-1]\).
-
type num_t
The number type aliasing double, i.e. double precision 64-bit floating point numbers, that holds double-precision normalized number in IEC 60559 in the approximative range \(\{-\infty\} \cup [-\text{huge}, -\text{tiny}] \cup \{0\} \cup [\text{tiny}, \text{huge}] \cup \{\infty\}\) where \(\text{huge} \approx 10^{308}\) and \(\text{tiny} \approx 10^{-308}\). See
MAD.constant.huge
andMAD.constant.tiny
for precise values.
-
type cpx_t
The complex type aliasing double _Complex, i.e. two double precision 64-bit floating point numbers, that holds double-precision normalized number in IEC 60559.
-
type str_t
The string type aliasing const char*, i.e. pointer to a readonly null-terminated array of characters.
-
type ptr_t
The pointer type aliasing const void*, i.e. pointer to readonly memory of unknown/any type.
Footnotes