lua-users home
lua-l archive

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


HyperHacker <hyperhacker@gmail.com> writes:
>> function callable (x)
>>  return type(x) == 'function' or getmetatable(x).__call
>> end
>
> That works if you only want to call it right now (maybe it's a callback for
> data you haven't received yet) and are willing to ignore any errors *other*
> than the object not being callable (or try to parse the error message). I
> don't recall offhand exactly what I was doing with that (and don't have
> access to the code at the moment) but there can be times you want to test
> for callability without actually calling.

Er, but that function _does_ (modulo bugs) "test for callability without
actually calling"...

If there were a standard builtin "callable" function, I imagine it would
do pretty much the same thing.

I agree that predicates like this are often better and more flexible
than using "type()" though (and I've argued so before on this list).

> My understanding too is that calling in protected mode is fairly expensive,
> and getmetatable() only works on tables

No, it's "setmetatable" that only works on tables; "getmetatable"
works on userdata etc too.

-Miles

-- 
Pray, v. To ask that the laws of the universe be annulled in behalf of a
single petitioner confessedly unworthy.