lua-users home
lua-l archive

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

On Wed, 1 Jun 2011, HyperHacker wrote:
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.
	I think there is no efficient way to check if a given object
is callable.  I would say that one should protect-call it and check if
it throws an error.  Thus you'd better not check it :-)