lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2013/5/29 Roberto Ierusalimschy <roberto@inf.puc-rio.br>:
>> [...] It was quite a shock
>> to discover that luaL_checkint does not actually check that
>> something is an integer.
>
> Nothing checks whether something is an "integer", because Lua does
> not have integers (yet ;). To check whether some value is a number,
> you can use lua_type(L, index) == LUA_TNUMBER. To check whether that
> number has an integer value, you can try
>
>   floor(lua_tonumber(L, index)) == lua_tonumber(L, index)

Well, what I really need to check is something much more restrictive:
whether an index into a "proper sequence" is in bounds. And that task
is beautifully simple in straight Lua.

1. Table is initialized with something other than nil (zero, NaN,
   whatever).
2. Metatable with __index, __newindex is assigned after that.
3. Any call that reaches __index or __newindex with a numeric key is
   by definition illegal (Lua "knows what to do" at the legal indices).

       x=Range(6)
       print(x) --> 1 2 3 4 5 6
       x[3]=60        -- no error
       x[math.pi]=180
    stdin:1: bad argument #2 to '__newindex' (index out of range)