lua-users home
lua-l archive

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


luaL_optstring and luaL_optnumber try to convert the value at the given index
to a string and number respectively. If luaL_optnumber does it with a string
that is not convertible it throws a "expected number, got string" error.

The Lua 5.1 and 5.2 manual say the following:
> lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number d);
>
> If the function argument narg is a number, returns this number. If this
> argument is absent or is nil, returns d. Otherwise, raises an error.

> const char *luaL_optstring (lua_State *L, int arg, const char *d);
>
> If the function argument arg is a string, returns this string. If this
> argument is absent or is nil, returns d. Otherwise, raises an error.

This reads as if the functions should not convert at all and always raise an
error instead. A note in the manual about this conversion could help developers
to be more aware of this behaviour.

I think changing the functions to follow the manual is probably not considered
as a solution, but I had to bring it up anyway. :)