Module mtype
Efficient enhanced types for Lua
There are two equal implementations of this module:
mtype.native
written in C, the most efficient option for PUC Lua (the official Lua interpreter),mtype.pure
written in pure Lua, the most efficient option for LuaJIT.
This module (mtype) detects if you run on LuaJIT (by presence of global
variable jit
) or PUC and returns mtype.pure
or mtype.native
.
Functions
type (value) | Returns (enhanced) type name of the given value. |
istype (typename, value) | Returns true if (enhanced) type of the given value meets the specified typename. |
Fields
_VERSION | Version of this module in format "x.y.z". |
Functions
- type (value)
-
Returns (enhanced) type name of the given value.
If the value is a table or an userdata with a metatable, then it looks for a metafield
__type
.If the metafield is a string, then it returns it as a type name.
If it's a function, then it calls it with the value as an argument. If the result is not nil, then it returns it as a type name; otherwise continues.
If the value is an IO userdata (file), then it calls io.type and returns result as a type name.
If nothing above applies, then it returns a raw type of the value, i.e. the same as built-in type function.
Parameters:
- value
Returns:
-
string
A type name of the value.
- istype (typename, value)
-
Returns true if (enhanced) type of the given value meets the specified typename. When called with only one argument, partially applied function is returned.
If raw type of the value equals the typename, then it returns true.
If the value is a table or an userdata with a metatable, then it looks for a metafield
__istype
.If the metafield is a function, then it calls it with arguments value, typename and returns the result (the return type should be boolean, but it's not checked!).
If a table, then it looks for a key that equals the typename. If there's no such key or its value is false (or nil), then it returns false. Otherwise returns true.
Otherwise raises error.
If nothing above applies, then it calls type with the value, compares the result with the typename and returns true if equals, false otherwise.
Parameters:
- typename string
- value
Returns:
-
boolean or function
Raises:
If typename is not a string or value is a table with metafield__istype
that is not a function or table.