lua-users home
lua-l archive

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


On 7/16/07, Hisham Muhammad <hisham.hm@gmail.com> wrote:
In my code I often put type() check assertions, but for public APIs I
believe the best practice would be to use check functions are
preferred over lua_type() checks, because non-string objects can be
used as strings (by having a __tostring function in their metatable)
-- no reason to restrict string parameters to "real strings" only.

I wasn't aware that luaL_checkstring() invokes the __tostring
metamethod? Or in other words: do lua_isstring() and lua_tolstring()
check for and invoke __tostring metamethods?

Perhaps the API spec could be improved to specify that whenever string
or number arguments are mentioned, the check functions should be used
and not lua_type enforcements.

I would like to see something like that in the docs yes: a note that
explains that when 'string' is mentioned it means 'any variable that
can be trivially coerced to a string i.e. for which lua_isstring()
returns true'. It might also be useful to advise the use of
luaL_checkstring()/luaL_checklstring() and the fact that it raises an
error when the conversion fails (not immediately clear from the
reference manual and indirectly mentioned in PiL2).

--
Dirk