Difference between revisions of "Metatable"
MKlegoman357 (Talk | contribs) (Improved) |
|||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
{{NeedsWork|Not enough info, perhaps incorrect info}} | {{NeedsWork|Not enough info, perhaps incorrect info}} | ||
| − | + | '''Metatables''' allow us to change the behavior of a {{type|table}}. For instance, using metatables, we can define how Lua computes the expression <code>a+b</code>, where <var>a</var> and <var>b</var> are tables. Whenever Lua tries to add two tables, it checks whether either of them has a metatable and whether that metatable has an <var>__add</var> field. If Lua finds this field, it calls the corresponding value (the so-called metamethod, which should be a {{type|function}}) to compute the sum. | |
| − | <!-- | + | <!-- Taken from the Lua PIL (http://www.lua.org/pil/13.html) --~~~~ --> |
== Setting and getting a metatable == | == Setting and getting a metatable == | ||
| − | + | To set the metatable of a table use <code>setmetatable( table, metatable )</code>, where <var>table</var> is the table which's metatable you want to set and <var>metatable</var> is the new metatable of that table. | |
| − | + | To get the metatable of a table use <code>getmetatable( table )</code>, where <var>table</var> is the table which's metatable you want to get. | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | is | + | |
| − | + | ||
| − | + | ||
| + | Note that the <var>__metatable</var> metafield can change the behavior of both above functions, see below for more info. | ||
| + | |||
== Metamethods == | == Metamethods == | ||
| − | A metamethod is a type of function which changes the default | + | A metamethod is a type of function which changes the default behavior of a table. The table below lists what each |
| − | one is called, and how each one is used. <br/> | + | one is called, and how each one is used.<br/> |
{| class="wikitable" | {| class="wikitable" | ||
| Line 25: | Line 21: | ||
|- | |- | ||
| __index | | __index | ||
| − | | | + | | Can be either a {{type|function}} or a {{type|table}}. Triggered whenever a table is being indexed and there is no field under the indexed <var>key</var>. If set to a table then that table is indexed using the same <var>key</var>. If it's a function then that function is called with the <var>table</var> and the <var>key</var> as parameters and its return value is used as the value under that <var>key</var>. |
| − | | | + | | the <var>table</var> and the <var>key</var> |
|- | |- | ||
| __newindex | | __newindex | ||
| − | | | + | | Triggered whenever a new (nil, non-existent) field is being assigned in a table. |
| − | | | + | | the <var>table</var>, the <var>key</var> and the new <var>value</var> |
|- | |- | ||
| __call | | __call | ||
| − | | | + | | Called whenever a table is being called as it was a function. |
| − | | | + | | the <var>table</var> and the arguments that where passed the the table-call expression. |
|- | |- | ||
| __metatable | | __metatable | ||
| − | | | + | | If present, <code>getmetatable</code> returns its value instead of the actual metatable, <code>setmetatable</code> raises an error. |
| − | | | + | | |
|- | |- | ||
| __mul | | __mul | ||
| − | | | + | | Triggered whenever a multiplication operation with a table occurs. Passes the two operands <var>a</var> and <var>b</var>, without telling which of the operands is the table and which is not. |
| − | + | | operand <var>a</var> and operand <var>b</var> | |
|- | |- | ||
| __div | | __div | ||
| − | | | + | | Triggered whenever a division operation with a table occurs. Behaves similar to <var>__mul</var> metamethod. |
| − | | | + | | operand <var>a</var> and operand <var>b</var> |
|- | |- | ||
| __sub | | __sub | ||
| − | | | + | | Triggered whenever a subtraction operation with a table occurs. Behaves similar to <var>__mul</var> metamethod. |
| − | | | + | | operand <var>a</var> and operand <var>b</var> |
|- | |- | ||
| __add | | __add | ||
| − | | | + | | Triggered whenever an addition operation with a table occurs. Behaves similar to <var>__mul</var> metamethod. |
| − | | | + | | operand <var>a</var> and operand <var>b</var> |
|- | |- | ||
| __mod | | __mod | ||
| − | | | + | | Triggered whenever a modulo operation with a table occurs. Behaves similar to <var>__mul</var> metamethod. |
| − | | | + | | operand <var>a</var> and operand <var>b</var> |
|- | |- | ||
| __pow | | __pow | ||
| − | | | + | | Triggered whenever a exponentiation (power) operation with a table occurs. Behaves similar to <var>__mul</var> metamethod. |
| − | | | + | | operand <var>a</var> and operand <var>b</var> |
|- | |- | ||
| __unm | | __unm | ||
| − | | | + | | Triggered whenever the unary ( <code>-table</code> ) operator is used on a table. |
| − | | | + | | the <var>table</var> |
|- | |- | ||
| __eq | | __eq | ||
| − | | | + | | Triggered whenever an "equal" ( == ) operation is performed on two tables with the exact same metatable. If the metatable does not match - returns false. |
| − | | | + | | operand <var>a</var> and operand <var>b</var> |
|- | |- | ||
| __lt | | __lt | ||
| − | | | + | | Triggered whenever a "less than" ( < ), or "more than" ( > ), operation is performed on the table. Similarly to the <var>__mul</var> metamethod it is not known which parameter is which. |
| − | | | + | | operand <var>a</var> and operand <var>b</var> |
|- | |- | ||
| __le | | __le | ||
| − | | | + | | Triggered whenever a "less than or equal to" ( <= ), or "less than or equal to" ( >= ), operation is performed on the table. Similarly to the <var>__mul</var> metamethod it is not known which parameter is which. |
| − | | | + | | operand <var>a</var> and operand <var>b</var> |
|- | |- | ||
| __concat | | __concat | ||
| − | | | + | | Triggered whenever a concatenation ( .. ) operation is performed on the table. Similarly to the <var>__mul</var> metamethod it is not known which parameter is which. |
| − | + | | operand <var>a</var> and operand <var>b</var> | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | | | + | |
|} | |} | ||
Latest revision as of 19:18, 12 May 2015
| This page needs some serious TLC, stat! Please help us by cleaning it, fixing it up, or sparing it some love.
(Reason: Not enough info, perhaps incorrect info) |
Metatables allow us to change the behavior of a table. For instance, using metatables, we can define how Lua computes the expression a+b, where a and b are tables. Whenever Lua tries to add two tables, it checks whether either of them has a metatable and whether that metatable has an __add field. If Lua finds this field, it calls the corresponding value (the so-called metamethod, which should be a function) to compute the sum.
Setting and getting a metatable
To set the metatable of a table use setmetatable( table, metatable ), where table is the table which's metatable you want to set and metatable is the new metatable of that table.
To get the metatable of a table use getmetatable( table ), where table is the table which's metatable you want to get.
Note that the __metatable metafield can change the behavior of both above functions, see below for more info.
Metamethods
A metamethod is a type of function which changes the default behavior of a table. The table below lists what each
one is called, and how each one is used.
| Name | Description | Arguments passed |
| __index | Can be either a function or a table. Triggered whenever a table is being indexed and there is no field under the indexed key. If set to a table then that table is indexed using the same key. If it's a function then that function is called with the table and the key as parameters and its return value is used as the value under that key. | the table and the key |
| __newindex | Triggered whenever a new (nil, non-existent) field is being assigned in a table. | the table, the key and the new value |
| __call | Called whenever a table is being called as it was a function. | the table and the arguments that where passed the the table-call expression. |
| __metatable | If present, getmetatable returns its value instead of the actual metatable, setmetatable raises an error.
|
|
| __mul | Triggered whenever a multiplication operation with a table occurs. Passes the two operands a and b, without telling which of the operands is the table and which is not. | operand a and operand b |
| __div | Triggered whenever a division operation with a table occurs. Behaves similar to __mul metamethod. | operand a and operand b |
| __sub | Triggered whenever a subtraction operation with a table occurs. Behaves similar to __mul metamethod. | operand a and operand b |
| __add | Triggered whenever an addition operation with a table occurs. Behaves similar to __mul metamethod. | operand a and operand b |
| __mod | Triggered whenever a modulo operation with a table occurs. Behaves similar to __mul metamethod. | operand a and operand b |
| __pow | Triggered whenever a exponentiation (power) operation with a table occurs. Behaves similar to __mul metamethod. | operand a and operand b |
| __unm | Triggered whenever the unary ( -table ) operator is used on a table.
|
the table |
| __eq | Triggered whenever an "equal" ( == ) operation is performed on two tables with the exact same metatable. If the metatable does not match - returns false. | operand a and operand b |
| __lt | Triggered whenever a "less than" ( < ), or "more than" ( > ), operation is performed on the table. Similarly to the __mul metamethod it is not known which parameter is which. | operand a and operand b |
| __le | Triggered whenever a "less than or equal to" ( <= ), or "less than or equal to" ( >= ), operation is performed on the table. Similarly to the __mul metamethod it is not known which parameter is which. | operand a and operand b |
| __concat | Triggered whenever a concatenation ( .. ) operation is performed on the table. Similarly to the __mul metamethod it is not known which parameter is which. | operand a and operand b |
Examples