lua-users home
lua-l archive

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


Peter Odding <peter@peterodding.com> writes:
>> 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

Technically yes, in that Lua will signal an error if you try to call
something with a non-callable __call MT entry, so that thing isn't
callable itself, even though it has a non-nil __call.

But frankly I don't think there's a whole lot of value in being so
pedantic about the issue; if somebody stores "3" in a __call MT entry,
as Mark says in his followup, they deserve whatever they get....

-Miles

-- 
On a bad day, I see brewers still talking as though all beer were
consumed in the pub, by the pint -- by insatiably thirsty Yorkshire
steelworkers who have in reality long ago sought work as striptease
artists.  [Michael Jackson]