[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Metamethods: fallbacks or overrides?
- From: Dirk Laurie <dirk.laurie@...>
- Date: Sun, 17 Aug 2014 19:42:48 +0200
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
Lua 5.3.0-alpha removes __ipairs, so it seems at this stage
that overrides is not a preferred direction for future Lua
development.
[1] <http://lua-users.org/lists/lua-l/2012-06/msg00246.html>