Difference between revisions of "Metatable"

From ComputerCraft Wiki
Jump to: navigation, search
(Took the intro from the PIL, it's a bit better. Also, note that it's 'Lua', not 'lua' or 'LUA'.)
Line 1: Line 1:
 
{{NeedsWork|Not enough info, perhaps incorrect info}}
 
{{NeedsWork|Not enough info, perhaps incorrect info}}
  
A metatable is an addition on to normal tables for adding metamethods, making classes as well as protecting functions. When a certain action is performed on a table, lua checks whether the corresponding __metatable field exists. For example, when you want to access the index bar in the table foo, but foo.bar does not exist, if the table has __index as a __metatable field, lua will call the __index field. If it's value is another table, lua will search for the index in the new table; however, if the value is a function, lua will pass the table name and the index as arguments.
+
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.
<!-- Possibly more, I don't know about any other application, unfortunately --~~~~ -->
+
<!-- Taken from the Lua PIL (http://www.lua.org/pil/13.html) --~~~~ -->
 
== Setting and getting a metatable ==
 
== Setting and getting a metatable ==
  

Revision as of 18:32, 12 May 2015

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

You will use setMetatable and getMetatable to set and get metatables.

You use getMetatable(table) to get your metatable from a table. It will return the __metatable metafield in the table.

setMetatable(table, metatable) to set your table's metatable. It will error if the __metatable metafield is set.

However, if the table has a metatable with the metafield __metatable, it will error when you use setMetatable and return what it is set to when getMetatable is called.

Metamethods

A metamethod is a type of function which changes the default way a table acts. The table below demonstrates what each one is called, and how each one is used.

Name Description Arguments passed
__index Used when table[variable] is called. The variable.
__newindex Used like __index, but it is only called when the variable matches an empty space in the table. The variable.
__call A metamethod to turn a table's name into a function, or whatever you want it to do. Function, Tuple
__metatable Used for protecting metatables, called when getmetatable() is used on the table, setmetatable() just errors. Nothing (as far as Unit158 knows)
__mul Used as an operator overloader for multiplication. The two objects being "multiplied".
__div Used as an operator overloader for division. The two objects being "divided".
__sub Used as an operator overloader for substraction. The two objects being "subtracted".
__add Used as an operator overloader for addition. The two objects being "added".
__mod Used as an operator overloader for modulo. The two objects being "divided".
__pow Used as an operator overloader for exponentiation operation. The two objects being exponentiated.
__unm I am not exactly sure how this one works, it is for negatives though. The table being changed into a negative.
__eq __eq is an operator overloader for comparison. The two objects being compared
__lt Less than. The two objects being compared
__le Less than or equal to. The objects being compared
__concat The objects being concatenated The two objects being concatenated
__len The # operator. The object having the operation performed on it.

Examples

Grid paper.png  Example
Use __eq to check if two tables are equal
Code
table1 = {x=0, y=1, z=0}
table2 = {x=1, y=1, z=0}
metatable = {__eq = function(operand1, operand2, type)
if operand1.x == operand2.x and operand1.y == operand2.y and operand1.z then --Note: In reality you might want to first check if your these variables actually exist
    return true
else
    return false
end }
setmetatable(table1, metatable)
setmetatable(table2, metatable)
if table1 == table2 then
    print("Equal")
else
    print("Not equal")
end



External links