Difference between revisions of "Lua: Metamethods"

From Mario Fan Games Galaxy Wiki
m
m
Line 29: Line 29:
 
*'''__tostring (table)''': called during an attempt to coerce the table into a string, such as by concatenating it with another string or by using the '''print''' function on it.
 
*'''__tostring (table)''': called during an attempt to coerce the table into a string, such as by concatenating it with another string or by using the '''print''' function on it.
 
*'''__call (table)''': called during an attempt to call a table as if it were a function.
 
*'''__call (table)''': called during an attempt to call a table as if it were a function.
*'''__metatable (table)''': essentially "locks" and hides the metatable for a given table so it can't be changed; functions like '''setmetatable''' will return the value of this metamethod instead of the actual metatable.
+
*'''__metatable''': essentially "locks" and hides the metatable for a given table so it can't be changed; functions like '''setmetatable''' will return the value of this instead of the actual metatable reference. Not actually a method.
 +
*'''__mode''': Changes the weakness setting of a table, which determines if the keys or values (or both) are ignored as references when the data they reference is collected. In other words, the garbage collector will collect an object even if weak references reference it. The value is a string, either "k", "v", or "kv" (or "vk"); if this field is nil, the table behaves normally. Not actually a method.
 
*'''__gc ()''': called for userdata, instructs Lua as to how to release other resources the userdata may be using.
 
*'''__gc ()''': called for userdata, instructs Lua as to how to release other resources the userdata may be using.
 
===Future Metamethods===
 
===Future Metamethods===

Revision as of 03:39, 8 November 2009

Lua
Lua.gif
Basics
Intermediate
Advanced
XLua
Add to this template
 Standardwikimessagebox.png This article assumes the use of Lua 5.1.

Information may not be accurate or may need revision if you are using a different version.

Metamethods are special variables (generally functions) that are called during some table and other datatype operations. They are important because they define the behavior of a table when Lua would otherwise not know how to handle it in that case. An operation between two tables may succeed even if one of the tables doesn't have the relevant metamethod, as Lua will use the metamethod of the table on the left side of the operand to decide the behavior; if that fails, it will try the one on the right—only when both attempts fail will it generate an error. In the case of conflicting metamethods, the one on the left will win out.

Although tables are not the only data structures that can have metatables, they are the only ones intended for programmers to change within Lua itself. Therefore, these lists will only make mention of their use on tables. It is important to note that a metamethod will not call other metamethods, but a single operation may cause more than one metamethod to be executed.

Metamethods should be "transparent" if applicable; that is, a given operation on tables should behave like it would with other datatypes.

Current Metamethods

As of Lua 5.1.4, also includes parameters passed to the metamethod

Arithmetic

  • __add (op1, op2): called during an attempt to perform addition (+) on a table. Must be a function.
  • __sub (op1, op2): called during an attempt to perform subtraction (-) on a table. Must be a function.
  • __mul (op1, op2): called during an attempt to perform multiplication (*) on a table. Must be a function.
  • __div (op1, op2): called during an attempt to perform division (/) on a table. Must be a function.
  • __mod (op1, op2): called during an attempt to perform modulus division (%) on a table. Must be a function.
  • __pow (op1, op2): called during an attempt to perform exponentiation (^) on a table. Must be a function.
  • __unm (op1): called during an attempt to negate (-) a table. Must be a function.
  • __concat(op1, op2): called during an attempt to concatenate (..) a table. Must be a function.

Comparison

  • __eq (op1, op2): called during an attempt to compare equal-to (==).
  • __lt (op1, op2): called during an attempt to compare less-than (<). Greater-than also calls this metamethod, but with the operands reversed.
  • __le (op1, op2): called during an attempt to compare less-than-or-equal-to (<=). Greater-than-or-equal-to also calls this metamethod, but with the operands reversed.

Indexing

  • __index (table, key): called during an attempt to access a nonexistent index. May be a function, or another table to look in instead. Bypassed by the rawget function.
  • __newindex (table, key, value): called during an attempt to write to a nonexistent index. Bypassed by the rawset function.

Other

  • __tostring (table): called during an attempt to coerce the table into a string, such as by concatenating it with another string or by using the print function on it.
  • __call (table): called during an attempt to call a table as if it were a function.
  • __metatable: essentially "locks" and hides the metatable for a given table so it can't be changed; functions like setmetatable will return the value of this instead of the actual metatable reference. Not actually a method.
  • __mode: Changes the weakness setting of a table, which determines if the keys or values (or both) are ignored as references when the data they reference is collected. In other words, the garbage collector will collect an object even if weak references reference it. The value is a string, either "k", "v", or "kv" (or "vk"); if this field is nil, the table behaves normally. Not actually a method.
  • __gc (): called for userdata, instructs Lua as to how to release other resources the userdata may be using.

Future Metamethods

Note: these have not totally been confirmed for Lua 5.2, so whether or not they will be implemented is unknown

  • __len (table): called during an attempt to use the length operator (#). Must be a function. Lua 5.2+