lua-users home
lua-l archive

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


Am 17.08.2014 um 19:42 schrieb Dirk Laurie:
Originally, metamethods were called "fallbacks". The first name
change, to "tag methods", came with Lua 3.0. In Lua 5.0, the
name in the manual was changed to "metamethods", but "tag
methods" still appears the code (ltm.h).

The idea, in the words of Roberto [1], is that:

    In general, arithmetic metamethods are only called when
     Lua "does not know what to do".

I.e. these metamethods are fallbacks. You no longer get an error,
you get the result of the metamethod.

In Lua 5.2, metamethods were introduced that are more like
overrides. Lua does know what to do, but you can tell it to
something else.

__len: preferred to the built-in code for the # operator
__pairs: preferred to the standard library "pairs" function
__ipairs: preferred to the standard library "ipairs" function

Don't forget:
__tostring
__gc
and to some extent
__index
__newindex
__eq


Lua 5.3.0-alpha removes __ipairs, so it seems at this stage
that overrides is not a preferred direction for future Lua
development.

In some cases overrides can't be avoided because Lua can't figure out if an operation is invalid (e.g. #t always returns a value even for non-sequences). The same is true for `tostring(v)`, `ipairs(t)`, and `pairs(t)`. Given the above list I guess Roberto's quote really is aimed at arithmetic metamethods only. I think it's pretty obvious the Lua authors want to remove `__ipairs` while still allowing sequence-like objects with embedded nils (that's why `__len` is involved). I honestly can't tell why.


[1] <http://lua-users.org/lists/lua-l/2012-06/msg00246.html>


Philipp