lua-users home
lua-l archive

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


On Jun 1, 2011, at 5:49 PM, Peter Odding wrote:

>>> 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 :-)
>> 
>> isn't it just
>> 
>> function callable (x)
>>  return type(x) == 'function' or getmetatable(x).__call
>> end
>> 
>> ?
> 
> Actually this makes me curious, I found the following in my local
> directory of modules and I'm wondering whether the recursion has a point
> or not?
> 
> function callable(value)
> if type(value) == 'function' then
>  return true
> else
>  local mt = getmetatable(value)
>  return type(mt) == 'table' and callable(mt.__call)
> end
> end

I think you deserve whatever happens to you if you put a non-callable value into __call...

Mark