lua-users home
lua-l archive

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

On 1-Feb-06, at 2:48 PM, Chris Marrin wrote:

Lua 5.1 has some nice argument checking functions, accessible from C, like luaL_checknumber() and luaL_optstring(). But these are not accessible from Lua, and I have not found anything roughly equivalent. It would be easy to write such things, of course, by doing type(), checking against nil and throwing errors. But is there any "standard" way to do this? I have not been able to find anything...

I usually write:

  a = assert(tonumber(a), "number expected")

or, in the case where there is a sensible default:

  a = assert(tonumber(a or sensible_default), "number expected")

This doesn't produce error messages as nice as luaL_*, though.

Also, it doesn't work with strings, since tostring(x) pretty well always produces a string, in constrast with lua_tostring() which only coerces numbers. It would sometimes be nice to have a Lua function which acted like lua_tostring() and a C function which acted like tostring(). Of course, it's easy enough to write both of those.