lua-users home
lua-l archive

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


Mark Hamburg wrote:
> It should probably be noted that this sort of trick only works for classes
> that don't expect to inherit any metamethods.

Yes, you are right. Thank you for the hint.

I guess I should rephrase that:
                  [...] And you need to override any methods from
  the superclass that may make the subclass userdata inconsistent.
  You must override or copy all metamethods from superclases
  since these are not inherited.

> Is there a particular reason why metamethod search doesn't obey __index
> links?


Metamethods are cached (or rather the non-existence of some of
them). This is done with a few bits in the table anchor structure
(of a metatable). A cache bit is set when a metamethod is
looked up, but not found. The cache is invalidated when the
metatable is modified.

This simple scheme no longer works with recursive metamethod
lookups. It would require an exhaustive search every time.
While this is not so bad for __index (which is the reason why
most tables have metatables in the first place), it's pretty
bad for __mode (needed by the GC on every traversal).