On Wed, Nov 19, 2014 at 4:26 PM, Thiago L. <fakedme@gmail.com> wrote:
<SoniEx2> %5.3 t=setmetatable({")", "'test'", "print("},{__call =
function(t,...) return table.remove(t) end}) load(t)()
<yalb> SoniEx2: sandbox:1: bad argument #1 to 'load' (function expected, got
table); stack traceback:; [C]: in function 'load'; sandbox:1: in main chunk
Can we please get a callable API? (for example, iscallable(obj), or in C,
lua_iscallable(whatever))
This is something I've brought up before (and seems to fit in with the
current int vs float discussion as well). In a lot of cases it's easy
and convenient to use Duck Typing in Lua. You don't check types, you
check capabilities (can this be called? does it have an "animate"
method?) or you just assume the objects are compatible and let an
error be thrown if not.
Lua already does this with metatables. It doesn't matter when you
write "x + y" if x and y are numbers, strings, tables, userdata; it
just tries to add them (using built-in logic or __add metamethod) and
if it can't, it complains.
Unfortunately it's not 100% possible to write code that never checks
or requires a specific type, even when it doesn't need to:
-Maybe you want to accept either a string or a function which
(hopefully) returns a string. Without a way to ask "is this object
callable?" you can only either use pcall (adding overhead) and handle
the error, or use a type check (so then custom objects won't be
usable).