Virus721
Virus721

Reputation: 8315

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.

Upvotes: 3

Views: 4126

Answers (3)

m2mm4m
m2mm4m

Reputation: 1

--  Lua 5.3 'type' replacement.
    local typeRaw = type;
    local typeNew = function(I_vValue)
        local LR_sType;
        local L_tyRaw=typeRaw(I_vValue);
        if (L_tyRaw == "number")then
            LR_sType = math.type(I_vValue);
        else
            LR_sType = L_tyRaw;
        end
        return LR_sType;
    end
--  _G.type=typeNew;    --    Over ride global.
    assert(typeNew(123)=="integer",     "Error with 'typeNew'.");
    assert(typeNew(123.456)=="float",   "Error with 'typeNew'.");
    assert(typeNew("123.456")=="string","Error with 'typeNew'.");
    assert(typeNew(nil)=="nil",         "Error with 'typeNew'.");

Upvotes: -1

ryanpattison
ryanpattison

Reputation: 6251

use math.type for Lua 5.3

Returns "integer" if x is an integer, "float" if it is a float, or nil if x is not a number.

Upvotes: 12

Rochet2
Rochet2

Reputation: 1156

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.

Upvotes: 1

Related Questions