Metatable
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