[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [PROPOSAL] more luaL_check* in lauxlib.h (for Lua 5.2)
- From: Patrick Rapin <toupie300@...>
- Date: Tue, 31 May 2011 15:25:58 +0200
I second the proposition.
Several times I tried to call such functions/macros, but the
completion engine of my IDE refused to complete the names :-)
These are only syntaxic sugar, so we can easily live without them, but
since it does not add any extra code to define these macros, I see no
good reason not to provide them.
I do not fully agree with Tony Finch remarks, since I have counter
examples for all points :
>> #define luaL_checkboolean(L,n) (luaL_checktype(L, (n), LUA_TBOOLEAN))
> Use lua_toboolean. It is more idiomatic than requiring that a value is
> exactly true or false.
In my recent binding of Pipeline (our proprietary image processing C++
library) to Lua, a C++ bool argument can only receive Lua true and
false values.
This is certainly easier to find errors in the user script if expected
boolean arguments are not forced to boolean values using
lua_toboolean.
For example 0 is true, although most probably the user would have
expected false. Raising an error is certainly clearer.
>> #define luaL_checknil(L,n) (luaL_checktype(L, (n), LUA_TNIL))
> Requiring a function argument to be nil doesn't make sense.
It makes little sense, I agree. Since, in the same Pipeline binding, I
placed such an assertion (to be precise: LUA_TNIL or LUA_TNONE) !
The use case is for callback functions. The C++ function pointer
parameter can accept a Lua function passed as argument, while the
associated void* context pointer must be passed nil, and is mapped to
a pointer to the Lua context.
>> #define luaL_checktable(L,n) (luaL_checktype(L, (n), LUA_TTABLE))
> Prevents callers from passing an object with a suitable metatable.
By object, do you mean userdata? A table with a metatable still has a
table type.
My use case for that is to perform a preliminary test for an numerical
array. The user of Pipeline for example has no way to build a userdata
acting like an array, and has no reason to do so.
So it makes sense to check for a table argument.