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 true if a

is_nil(a)

is a nil

is_boolean(a)

is a boolean

is_number(a)

is a number

is_string(a)

is a string

is_function(a)

is a function

is_table(a)

is a table

is_userdata(a)

is a userdata

is_coroutine(a)

is a thread [1]

is_cdata(a)

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 true if a

is_nan(a)

is nan (Not a Number)

is_true(a)

is true

is_false(a)

is false

is_logical(a)

is a boolean or nil

is_finite(a)

is a number with \(|a| < \infty\)

is_infinite(a)

is a number with \(|a| = \infty\)

is_positive(a)

is a number with \(a > 0\)

is_negative(a)

is a number with \(a < 0\)

is_zpositive(a)

is a number with \(a \ge 0\)

is_znegative(a)

is a number with \(a \le 0\)

is_nonzero(a)

is a number with \(a \ne 0\)

is_integer(a)

is a number with \(-2^{52} \le a \le 2^{52}\) and no fractional part

is_int32(a)

is a number with \(-2^{31} \le a < 2^{31}\) and no fractional part

is_natural(a)

is an integer with \(a \ge 0\)

is_nznatural(a)

is an integer with \(a > 0\)

is_even(a)

is an even integer

is_odd(a)

is an odd integer

is_decimal(a)

is not an integer

is_emptystring(a)

is a string with #a == 0

is_identifier(a)

is a string with valid identifier characters, i.e. %s*[_%a][_%w]*%s*

is_rawtable(a)

is a table with no metatable

is_emptytable(a)

is a table with no element

is_file(a)

is a userdata with io.type(a) ~= nil

is_openfile(a)

is a userdata with io.type(a) == 'file'

is_closedfile(a)

is a userdata with io.type(a) == 'closed file'

is_emptyfile(a)

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 true if a

is_value(a)

is a nil, a boolean, a number or a string

is_reference(a)

is not a value

is_empty(a)

is a mappable and 1st iteration returns nil

is_lengthable(a)

supports operation #a

is_iterable(a)

supports operation ipairs(a)

is_mappable(a)

supports operation pairs(a)

is_indexable(a)

supports operation a[?]

is_extendable(a)

supports operation a[]=?

is_callable(a)

supports operation a()

is_equalable(a)

supports operation a == ?

is_orderable(a)

supports operation a < ?

is_concatenable(a)

supports operation a .. ?

is_negatable(a)

supports operation -a

is_addable(a)

supports operation a + ?

is_subtractable(a)

supports operation a - ?

is_multipliable(a)

supports operation a * ?

is_dividable(a)

supports operation a / ?

is_modulable(a)

supports operation a % ?

is_powerable(a)

supports operation a ^ ?

is_copiable(a)

supports metamethod __copy()

is_sameable(a)

supports metamethod __same()

is_tablable(a)

supports metamethod __totable()

is_stringable(a)

supports metamethod __tostring()

is_mutable(a)

defines metamethod __metatable()

is_restricted(a)

has metamethods for restriction, see wrestrict()

is_protected(a)

has metamethods for protection, see wprotect()

is_deferred(a)

has metamethods for deferred expressions, see deferred()

is_same(a,b)

has the same type and metatable as b

The functions in the following table are complementary to concepts and usually used to prevent an error during concepts checks.

Functions

Return true if

has_member(a,b)

a[b] is not nil

has_method(a,f)

a[f] is a callable

has_metamethod(a,f)

metamethod f is defined

has_metatable(a)

a has a metatable

is_metaname(a)

Returns true if the string a is a valid metamethod name, false otherwise.

get_metatable(a)

Returns the metatable of a even if a is a cdata, which is not the case of getmetatable().

get_metamethod(a, f)

Returns the metamethod (or method) f of a even if a is a cdata and f is only reachable through the metatable, or nil.

Setting Concepts

typeid.concept

The table concept contains the lists of concepts that can be passed to the function set_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

not_lengthable

__len

not_iterable

__ipairs

not_mappable

__ipairs and __pairs

not_scannable

__len, __ipairs and __pairs

not_indexable

__index

not_extendable

__newindex

not_callable

__call

not_equalable

__eq

not_orderable

__lt and __le

not_comparable

__eq, __lt and __le

not_concatenable

__concat

not_copiable

__copy and __same

not_tablable

__totable

not_stringable

__tostring

not_mutable

__metatable and __newindex

not_negatable

__unm

not_addable

__add

not_subtractable

__sub

not_additive

__add and __sub

not_multipliable

__mul

not_dividable

__div

not_multiplicative

__mul and __div

not_modulable

__mod

not_powerable

__pow

set_concept(mt, concepts, strict_)

Return the metatable mt after setting the metamethods associated to the combination of concepts set in concepts 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 if strict = false, otherwise the overload is silently discarded. If concepts requires iterable but not mappable then pairs is equivalent to ipairs.

wrestrict(a)

Return a proxy for a which behaves like a, 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 like a, 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 protected a.

deferred(a)

Return a proxy for a which behaves like a 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

ctsz_log

log_t

ctsz_idx

idx_t

ctsz_ssz

ssz_t

ctsz_dbl

num_t

ctsz_cpx

cpx_t

ctsz_str

str_t

ctsz_ptr

ptr_t

C API

type log_t

The logical type aliasing _Bool, i.e. boolean, that holds TRUE or FALSE.

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 and MAD.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