lua-users home
lua-l archive

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


2010/11/27 Julien Cugnière <julien.cugniere@gmail.com>:
> The check* are for required argument : they will raise a friendly
> error message if the argument is nil/omitted, or of the wrong type.
> The opt* functions return the default value you specify if the
> parameter is omitted/nil. Your example function would be written as
> follow :
>
> int myfunc(lua_State* L)
> {
>    const char* s = luaL_checkstring(L, 1);
>    int a = luaL_optint(L, 2, 0);
>    double b = luaL_optnumber(L, 3, 0.0);
>    ....
>    return 0;
> }
>
> These functions are simple and invaluable. If you need to write
> similar functions for your own types (and you should :-), there are
> utility functions such as luaL_checktype, luaL_argcheck or
> luaL_argerror.

Thank you Julien, but here is one doubt I have: can I use luaL_optint
on the stack element 2 without checking lua_gettop before ?

What you are suggesting is the equivalent of lua_isnoneornil plus some
checks and nice error messages but before Drake was suggesting to use
lua_gettop instead, at the beginning of the C function.

You see my doubt now, may be... :-)

-- 
Francesco