[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: __call metamethod, misleading doc or am I missing something?
- From: Daurnimator <quae@...>
- Date: Tue, 21 Feb 2017 17:38:34 +1100
On 21 February 2017 at 17:26, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> 2017-02-20 14:15 GMT+02:00 Francisco Olarte <folarte@peoplecall.com>:
>
>> Reading "2.4 – Metatables and Metamethods" the only hint I get of
>> metatable methods needing to be functions is the end of the first
>> paragraph " Lua checks for a function in the field "__add" of the
>> value's metatable. If it finds one, Lua calls this function to perform
>> the addition. ".
> ...
>> So, if the metamethod for __call NEEDS to be a real function, I think
>> it should be clearly indicated in its documentation.
>
> The blanket requiremend is that _all_ metamethods should be
> functions.
>
> The two exceptions (__index and __newindex) are very clearly
> documented. In the first case, it says "Despite the name, the
> metamethod for this event can be either a function or a table."
>
> Since at the entry for __call does not say anything of the kind,
> the implication is that the metamethod cannot be a table.
The key thing here is use of a function `type(x) == "function" vs
using a call-able, i.e. an `x()` that does something.
Lua currently checks the former; but perhaps it should more permissive?
Being able to treat a table as a function is useful in many contexts.
This *should* be as easy as removing the `if (!ttisfunction(tm))`
check in https://www.lua.org/source/5.3/ldo.c.html#tryfuncTM
However as you noted: __index and __newindex already have special
behaviour. if you wrote:
callable = setmetatable({foo="bar"}, {__call=function() print("callable") end})
proxy_table = setmetatable({}, {__index=callable})
do you expect callable to be indexed or called?
If so: this would mean a "callable" can be used almost anywhere,
*except* as an __index metamethod (or __newindex, or anywhere that
checks type()) which might be more confusing than the status quo?
I think it's an interesting suggestion at least, and I think I could
be swayed either way.