cluainteger

Lua 5.3 - Integers - type() - lua_type()


Since Lua 5.3, inegers are supported.

But how can I do :

if type( 123 ) == "integer" then
end

Or

switch( lua_type( L, -1 ) )
{
case LUA_TINTEGER:
    break;
}

Since type() is still going to return "number" for both integer and reals, and LUA_TINTEGER does not exist ?

Thanks.


Solution

  • In Lua 5.3 you can use math.tointeger to check if the value is an integer. http://www.lua.org/manual/5.3/manual.html#pdf-math.tointeger

    If the value x is convertible to an integer, returns that integer. Otherwise, returns nil.

    In C you can use lua_isinteger for the same purpose. http://www.lua.org/manual/5.3/manual.html#lua_isinteger

    Returns 1 if the value at the given index is an integer (that is, the value is a number and is represented as an integer), and 0 otherwise.