lua-users home
lua-l archive

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


On Sat, Jun 13, 2009 at 7:26 PM, Steven Johnson wrote:
>> There should be discussion about whether this should first look at
>> rawget( t, k ) before turning to the metamethods.
> I do find it handy to be able to override an instance's method, sometimes only
> temporarily, ... Of course, while this use case favors doing the lookup first, it's
> probably not a very important one, as I doubt it's much more difficult to add
> the handling into the mcall() itself.

In the set ADT example above, storing "union" in the set would break
the union method.  rawget(o, 'k') would determine behavior of both o.k
and o:k.  The typical solution would then be for the object to instead
store its private data in another table proxied through __index.

To bring up the method-operator analogy again--note also that we
normally lack a handy way to override an individual instance's
operators, though you could monkey patch the getmetatable(obj).__mcall
table (affects all objects that use that metatable).

BTW, even with the __mcall proposal, we remain unable to define
"properties" on the set ADT above since, for example, set.empty and
set['empty'] are equivalent, but that we might ignore.