lua-users home
lua-l archive

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


On May 31, 2011 9:01 AM, "Javier Guerra Giraldez" <javier@guerrag.com> wrote:
>
> On Tue, May 31, 2011 at 8:25 AM, Patrick Rapin <toupie300@gmail.com> wrote:
> > For example 0 is true, although most probably the user would have
> > expected false. Raising an error is certainly clearer.
>
> not me; i like my 0s and ''s to remain non-falsy
>
> --
> Javier
>

I've often found myself wondering why there's no luaL_optbool. I do sometimes have arguments that default to true. I suppose some might argue I shouldn't do that because nil == false and missing arg == nil, but I usually treat missing args as "fall back to default", and if you want false, just say so by passing false. :)

The point about explicitly checking for a table is also something that's bugged me a bit in Lua. Dynamically-typed languages like Lua are ideal for Duck Typing - don't ask what an object is, just check if it satisfies your needs. (Or assume so and let the caller deal with the resulting error if not.) Whether x>0 really isn't related to whether x is a number or some object having a __gt metamethod.
Lua's native API, both C-side and Lua-side, unfortunately don't offer much in this regard. There's no way I know of to ask "is x callable?" other than testing if either it's a function or has __call. No way to ask "is x indexable?" other than a similar check or actually trying to index it and seeing if an error is thrown (and hoping your test access doesn't cause any side effects, say if x does some heavy computation and/or caching to return its values).
I think it'd be much more efficient if Lua provided some functions to test if a given object supports various actions - being called, indexed, being used as a number or string, being used in a greater/less-than compare to some other object, etc.

(PS sorry if this message is ugly. The mobile Gmail client has improved a lot in recent versions, but might still have some quirks I don't know about. :)

--
Sent from my toaster.