Difference between revisions of "Lua: Metamethods"
m |
m |
||
Line 2: | Line 2: | ||
'''Metamethods''' are special variables (generally functions) that are called during some [[Lua: Tables|table]] and other datatype operations. They are important because they define the behavior of a table when [[Lua]] would otherwise not know how to handle it in that case. An operation between two tables may succeed even if one of the tables doesn't have the relevant metamethod, as Lua will use the metamethod of the table on the left side of the operand to decide the behavior; if that fails, it will try the one on the right{{emdash}}only when both attempts fail will it generate an error. In the case of conflicting metamethods, the one on the left will win out. | '''Metamethods''' are special variables (generally functions) that are called during some [[Lua: Tables|table]] and other datatype operations. They are important because they define the behavior of a table when [[Lua]] would otherwise not know how to handle it in that case. An operation between two tables may succeed even if one of the tables doesn't have the relevant metamethod, as Lua will use the metamethod of the table on the left side of the operand to decide the behavior; if that fails, it will try the one on the right{{emdash}}only when both attempts fail will it generate an error. In the case of conflicting metamethods, the one on the left will win out. | ||
− | Although tables are not the only data structures that can have metatables, they are the only ones | + | Although tables are not the only data structures that can have metatables (in fact all of them can, even <tt>nil</tt>), they are the only ones that can have metatables added or removed without using the debug library. However, though it is possible to modify, for example, the strings' metatable without the debug library, some behavior such as concatenation is hardcoded and cannot be overridden. |
− | + | Whenever possible, overridden operators should have meaning that is predictable or similar to what is normally understood for that operator. Therefore, you may consider using <tt>+</tt> for field-wise addition rather than concatenation, using <tt>..</tt> for the latter operation. | |
+ | |||
+ | Metamethods are also known as ''metafields'', which is a more accurate term for the few that are in fact just normal values and not functions. | ||
===Current Metamethods=== | ===Current Metamethods=== | ||
''As of Lua 5.1.4, also includes parameters passed to the metamethod'' | ''As of Lua 5.1.4, also includes parameters passed to the metamethod'' | ||
+ | {| style="border: 2px solid #aac; background-color: #fff; font-size: 90%; text-align: center;" cellpadding="3" | ||
+ | |- style="font-size:110%; text-align: center; background-color: #ccccff;" | ||
+ | ! Name | ||
+ | ! Kind | ||
+ | ! Arguments/Value | ||
+ | ! Triggered by | ||
+ | ! Notes | ||
+ | |- | ||
+ | ! __add | ||
+ | | Method | ||
+ | | (self, right) | ||
+ | | ''self'' + ''right'' | ||
+ | | | ||
+ | |- | ||
+ | ! __sub | ||
+ | | Method | ||
+ | | (self, right) | ||
+ | | ''self'' - ''right'' | ||
+ | | | ||
+ | |- | ||
+ | ! __mul | ||
+ | | Method | ||
+ | | (self, right) | ||
+ | | ''self'' * ''right'' | ||
+ | | | ||
+ | |- | ||
+ | ! __div | ||
+ | | Method | ||
+ | | (self, right) | ||
+ | | ''self'' / ''right'' | ||
+ | | | ||
+ | |- | ||
+ | ! __mod | ||
+ | | Method | ||
+ | | (self, right) | ||
+ | | ''self'' % ''right'' | ||
+ | | | ||
+ | |- | ||
+ | ! __pow | ||
+ | | Method | ||
+ | | (self, right) | ||
+ | | ''self'' * ''right'' | ||
+ | | | ||
+ | |- | ||
+ | ! __unm | ||
+ | | Method | ||
+ | | (self) | ||
+ | | -''self'' | ||
+ | | | ||
+ | |- | ||
+ | ! __concat | ||
+ | | Method | ||
+ | | (self, right) | ||
+ | | ''self'' .. ''right'' | ||
+ | | | ||
+ | |- | ||
+ | ! __eq | ||
+ | | Method | ||
+ | | (self, right) | ||
+ | | ''self'' == ''right'' | ||
+ | | 1 | ||
+ | |- | ||
+ | ! __lt | ||
+ | | Method | ||
+ | | (self, right) | ||
+ | | ''self'' < ''right'' | ||
+ | | 2 | ||
+ | |- | ||
+ | ! __le | ||
+ | | Method | ||
+ | | (self, right) | ||
+ | | ''self'' <= ''right'' | ||
+ | | 3 | ||
+ | |- | ||
+ | ! __index | ||
+ | | Method | ||
+ | | (self, key) | ||
+ | | ''self''[''key''] | ||
+ | | 4, 5 | ||
+ | |- | ||
+ | ! __newindex | ||
+ | | Method | ||
+ | | (self, key, value) | ||
+ | | ''self''[''key''] = ''value'' | ||
+ | | 4, 6 | ||
+ | |- | ||
+ | ! __tostring | ||
+ | | Method | ||
+ | | (self) | ||
+ | | tostring(''self'') | ||
+ | | | ||
+ | |- | ||
+ | ! __call | ||
+ | | Method | ||
+ | | (self, ...) | ||
+ | | ''self''(...) | ||
+ | | 7 | ||
+ | |- | ||
+ | ! __metatable | ||
+ | | Any value | ||
+ | | [returned value] | ||
+ | | getmetatable(''self'') | ||
+ | | | ||
+ | |- | ||
+ | ! __mode | ||
+ | | String | ||
+ | | "k", "v", or "kv" | ||
+ | | | ||
+ | | | ||
+ | |- | ||
+ | ! __gc | ||
+ | | Method | ||
+ | | (self) | ||
+ | | [when collectible] | ||
+ | | 8 | ||
+ | |- | ||
+ | ! __len | ||
+ | | Method | ||
+ | | (self) | ||
+ | | #''self'' | ||
+ | | 9 | ||
+ | |- | ||
+ | ! __pairs | ||
+ | | Method | ||
+ | | (self) | ||
+ | | pairs(''self'') | ||
+ | | 9 | ||
+ | |- | ||
+ | ! __ipairs | ||
+ | | Method | ||
+ | | (self) | ||
+ | | ipairs(''self'') | ||
+ | | 9 | ||
+ | |- | ||
+ | |} | ||
+ | # Operands must be the same type and share the same <tt>__eq</tt> metamethod. Bypassable by <tt>rawequal()</tt> | ||
+ | # Also called by <tt>''self'' > ''right''</tt>, but with the operands flipped. Operands must be same type | ||
+ | # Also called by <tt>''self'' >= ''right''</tt>, but with the operands flipped. Operands must be same type | ||
+ | # Triggered only if the specified <tt>key</tt> does not exist | ||
+ | # Bypassable by <tt>rawget()</tt> | ||
+ | # Bypassable by <tt>rawset()</tt> | ||
+ | # <tt>__call</tt> may have extra parameters after the <tt>self</tt> reference | ||
+ | # Userdata only, used to free resources allocated by the userdata before collection | ||
+ | # Lua 5.2 required | ||
− | + | Further details of <tt>__metatable</tt> and <tt>__mode</tt> are specified on the [[Lua: Metatables|Metatables]] and [[Lua: Weak Tables|Weak Tables]] pages, respectively. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Latest revision as of 23:21, 2 April 2010
Lua |
---|
Basics |
Intermediate |
Advanced |
|
XLua |
Add to this template |
Metamethods are special variables (generally functions) that are called during some table and other datatype operations. They are important because they define the behavior of a table when Lua would otherwise not know how to handle it in that case. An operation between two tables may succeed even if one of the tables doesn't have the relevant metamethod, as Lua will use the metamethod of the table on the left side of the operand to decide the behavior; if that fails, it will try the one on the right—only when both attempts fail will it generate an error. In the case of conflicting metamethods, the one on the left will win out.
Although tables are not the only data structures that can have metatables (in fact all of them can, even nil), they are the only ones that can have metatables added or removed without using the debug library. However, though it is possible to modify, for example, the strings' metatable without the debug library, some behavior such as concatenation is hardcoded and cannot be overridden.
Whenever possible, overridden operators should have meaning that is predictable or similar to what is normally understood for that operator. Therefore, you may consider using + for field-wise addition rather than concatenation, using .. for the latter operation.
Metamethods are also known as metafields, which is a more accurate term for the few that are in fact just normal values and not functions.
Current Metamethods
As of Lua 5.1.4, also includes parameters passed to the metamethod
Name | Kind | Arguments/Value | Triggered by | Notes |
---|---|---|---|---|
__add | Method | (self, right) | self + right | |
__sub | Method | (self, right) | self - right | |
__mul | Method | (self, right) | self * right | |
__div | Method | (self, right) | self / right | |
__mod | Method | (self, right) | self % right | |
__pow | Method | (self, right) | self * right | |
__unm | Method | (self) | -self | |
__concat | Method | (self, right) | self .. right | |
__eq | Method | (self, right) | self == right | 1 |
__lt | Method | (self, right) | self < right | 2 |
__le | Method | (self, right) | self <= right | 3 |
__index | Method | (self, key) | self[key] | 4, 5 |
__newindex | Method | (self, key, value) | self[key] = value | 4, 6 |
__tostring | Method | (self) | tostring(self) | |
__call | Method | (self, ...) | self(...) | 7 |
__metatable | Any value | [returned value] | getmetatable(self) | |
__mode | String | "k", "v", or "kv" | ||
__gc | Method | (self) | [when collectible] | 8 |
__len | Method | (self) | #self | 9 |
__pairs | Method | (self) | pairs(self) | 9 |
__ipairs | Method | (self) | ipairs(self) | 9 |
- Operands must be the same type and share the same __eq metamethod. Bypassable by rawequal()
- Also called by self > right, but with the operands flipped. Operands must be same type
- Also called by self >= right, but with the operands flipped. Operands must be same type
- Triggered only if the specified key does not exist
- Bypassable by rawget()
- Bypassable by rawset()
- __call may have extra parameters after the self reference
- Userdata only, used to free resources allocated by the userdata before collection
- Lua 5.2 required
Further details of __metatable and __mode are specified on the Metatables and Weak Tables pages, respectively.