Next: 4.8 Error Handling Up: 4 The Language Previous: 4.6 Function Definitions

4.7 Fallbacks

Lua provides a powerful mechanism to extend its semantics, called fallbacks. Basically, a fallback is a programmer defined function which is called whenever Lua does not know how to proceed.

Lua supports the following fallbacks, identified by the given strings:

``arith''
called when an arithmetic operation is applied to non numerical operands, or when the binary ^ operation is called. Receives three arguments: the two operands (the second one is nil when the operation is unary minus) and one of the following strings describing the offended operator:
  add  sub  mul  div  pow  unm
Its return value is the final result of the arithmetic operation. The default function issues an error.
``order''
called when an order comparison is applied to non numerical or non string operands. Receives three arguments: the two operands and one of the following strings describing the offended operator:
  lt gt le ge
Its return value is the final result of the comparison operation. The default function issues an error.
``concat''
called when a concatenation is applied to non string operands. Receives the two operands as arguments. Its return value is the final result of the concatenation operation. The default function issues an error.
``index''
called when Lua tries to retrieve the value of an index not present in a table. Receives as arguments the table and the index. Its return value is the final result of the indexing operation. The default function returns nil.
``gettable''
called when Lua tries to index a non table value. Receives as arguments the non table value and the index. Its return value is the final result of the indexing operation. The default function issues an error.
``settable''
called when Lua tries to assign indexed a non table value. Receives as arguments the non table value, the index, and the assigned value. The default function issues an error.
``function''
called when Lua tries to call a non function value. Receives as arguments the non function value and the arguments given in the original call. Its return values are the final results of the call operation. The default function issues an error.
``gc''
called during garbage collection. Receives as argument the table being collected. After each run of the collector this function is called with argument nil. Because this function operates during garbage collection, it must be used with great care, and programmers should avoid the creation of new objects (tables or strings) in this function. The default function does nothing.
``error''
called when an error occurs. Receives as argument a string describing the error. The default function prints the message on the standard error output.

The function setfallback is used to change a fallback action. Its first argument is a string describing the fallback, and the second the new function to be called. It returns the old function for the given fallback, or nil on error.

Section 7.4 shows an example of the use of fallbacks.

Next: 4.8 Error Handling Up: 4 The Language Previous: 4.6 Function Definitions