The following keywords are reserved and cannot be used as names:
and break doelse elseif endfalseforfunctiongotoifinlocal nil notorrepeatreturnthentrueuntilwhile
A detailed list of events controlled by metatables is given next. Each operation is identified by its corresponding key.
__add: the addition (+) operation. If any operand for an addition is not a number (nor a string coercible to a number), Lua will try to call a metamethod. First, Lua will check the first operand (even if it is valid). If that operand does not define a metamethod for __add, then Lua will check the second operand. If Lua can find a metamethod, it calls the metamethod with the two operands as arguments, and the result of the call (adjusted to one value) is the result of the operation. Otherwise, it raises an error.
__sub: the subtraction (-) operation. Behavior similar to the addition operation.
__mul: the multiplication (*) operation. Behavior similar to the addition operation.
__div: the division (/) operation. Behavior similar to the addition operation.
__mod: the modulo (%) operation. Behavior similar to the addition operation.
__pow: the exponentiation (^) operation. Behavior similar to the addition operation.
__unm: the negation (unary -) operation. Behavior similar to the addition operation.
__idiv: the floor division (//) operation. Behavior similar to the addition operation.
__band: the bitwise AND (&) operation. Behavior similar to the addition operation, except that Lua will try a metamethod ifany operand is neither an integer nor a value coercible to an integer (see §3.4.3).
__bor: the bitwise OR (|) operation. Behavior similar to the bitwise AND operation.
__bxor: the bitwise exclusive OR (binary ~) operation. Behavior similar to the bitwise AND operation.
__bnot: the bitwise NOT (unary ~) operation. Behavior similar to the bitwise AND operation.
__shl: the bitwise left shift (<<) operation. Behavior similar to the bitwise AND operation.
__shr: the bitwise right shift (>>) operation. Behavior similar to the bitwise AND operation.
__concat: the concatenation (..) operation. Behavior similar to the addition operation, except that Lua will try a metamethod ifany operand is neither a string nor a number (which is always coercible to a string).
__len: the length (#) operation. If the object isnot a string, Lua will try its metamethod. If there is a metamethod, Lua calls it with the object as argument, and the result of the call (always adjusted to one value) is the result of the operation. If there isno metamethod but the object is a table, then Lua uses the table length operation (see §3.4.7). Otherwise, Lua raises an error.
__eq: the equal (==) operation. Behavior similar to the addition operation, except that Lua will try a metamethod onlywhen the values being compared are either both tables orbothfull userdata and they arenot primitively equal. The result of the callis always converted to a boolean.
__lt: the less than (<) operation. Behavior similar to the addition operation, except that Lua will try a metamethod onlywhen the values being compared are neither both numbers nor both strings. The result of the callis always converted to a boolean.
__le: the less equal (<=) operation. Unlike other operations, the less-equal operation can use two different events. First, Lua looks for the __le metamethod inboth operands, likein the less than operation. If it cannot find such a metamethod, then it will try the __lt metamethod, assuming that a <= b is equivalent tonot (b < a). Aswith the other comparison operators, the result is always a boolean. (This use of the __lt event can be removed in future versions; it is also slower than a real __le metamethod.)
__index: The indexing access table[key]. This event happens when table is not a table or when key is not present in table. The metamethod is looked up in table.
Despite the name, the metamethod for this event can be either a function or a table. If it is a function, it is called with table and key as arguments, and the result of the call (adjusted to one value) is the result of the operation. If it is a table, the final result is the result of indexing this tablewithkey. (This indexing is regular, not raw, and therefore can trigger another metamethod.)
__newindex: The indexing assignment table[key] = value. Like the index event, this event happens whentableisnot a tableorwhenkeyisnot present intable. The metamethod is looked up intable.
Likewith indexing, the metamethod for this event can be either a function or a table. If it is a function, it is called withtable, key, andvalueas arguments. If it is a table, Lua does an indexing assignment to this tablewith the same keyandvalue. (This assignment is regular, not raw, and therefore can trigger another metamethod.)
Whenever there is a __newindex metamethod, Lua does not perform the primitive assignment. (If necessary, the metamethod itself can call rawset todo the assignment.)
__call: The call operation func(args). This event happens when Lua tries tocall a non-function value (that is, func isnot a function). The metamethod is looked up in func. If present, the metamethod is called with func as its first argument, followed by the arguments of the original call (args). All results of the callare the result of the operation. (This is the only metamethod that allows multiple results.)
A weak table is a table whose elements are weak references. A weak referenceis ignored bythe garbage collector. In other words, ifthe only references to an object are weak references, thenthe garbage collector will collect that object.
The weakness of a table is controlled bythe __mode field ofits metatable. If the __mode field is a string containing thecharacter 'k', the keys inthe table are weak. If __mode contains 'v', the values inthe table are weak.