lua-users home
lua-l archive

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

Gaspard Bucher wrote:
> What is the simplest way to check a return value for false, doing the
> equivalent of the Lua code:
> val == false
> but with the C API...
> My best guess for the moment is:
> lua_isboolean(L, -1) && !lua_toboolean(L, -1)

Hi, I beleive there is no shorter way. But it's common in Lua to write
"if not val" instead of "if val == false", thus also allowing nil as a
false value. The C equivalent of the latter is just "if (!
lua_toboolean(L, -1))". If you want to enforce type checking, it's
better to throw an error separately:
   if (!lua_isboolean(L, -1))
     luaL_error("Expected boolean for the last argument, got %s", 
                 luaL_typename(L, -1)");

-- Gregory