[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: luaL_checkboolean() equivalent?
- From: Peter Odding <peter@...>
- Date: Fri, 22 Jul 2011 21:10:41 +0200
> I'm new to the Lua C interface and wonder if comeone can suggest
> the best Lua idiom to receive a boolean parameter into a C function?
>
> I was hoping for something like:
>
> int self_destruct( lua_state *L )
> {
> bool are_you_sure = luaL_checkboolean( L, 1 );
>
> but luaL_checkboolean doesn't exist. Of course, it couldn't fail with
> a type error, because nil and false are false, while true and
> everything else are true, but do I really have to check the type of
> the parameter and check for nil and false explicitly, with some garply
> like:
>
> bool are_you_sure = ! ( lua_isnil( L, 1 ) || (lua_isboolean( L, 1 )
> && ! lua_toboolean( L, 1 ) );
>
> or is there a cleaner way?
I'd say just go with the flow of Lua (that is to say everything except
nil and false is true) and use the following:
bool are_you_sure = lua_toboolean(L, 1);
Insisting on a value of type boolean doesn't add any value IMHO; Lua
generally doesn't do strict type checking so why bother with it here?
- Peter