lua-users home
lua-l archive

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


>> 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

 - Peter