Archive - Originally posted on "The Horse's Mouth" - 2012-04-12 07:16:24 - Graham Ellis
In Lua, you can associate an additional table (known as a metatable) with each table of data and code you have. By doing so, repeated values and functions that apply across as whole series ("type", "class") of tables only have to be defined once. A very neat way for Lua to provide the OO facilities within a lightweight language.
Metatables are provided with a number of hooks, starting with __ (double underscore) which provide special functionallity / syntactic icing within the language; I believe that the following is a comprehensive list of them for Lua 5.2
__gc - finaliser (prior to garbage collection; the destructor)
__mode - setting keys or values to be weak
__add - addition
__sub - subtraction
__mul - multiplication
__div - division
__mod - modulo
__pow - raise to power
__unm - Unary Minus
__len - the # operator and the len function
__concat - concatenation (the .. operator)
__eq - equal
__lt - less than
__le - less than or equal
__index - Accessing a table element
__newindex - Creating a table element
__call - calling the code in a table element
__tostring - conversion to string (tostring and print)
__ipairs - the ipairs function
__pairs - the pairs function