Reputation: 1
i know that it is possible to overload the addition operator in lua for tables. By doing:
foo = {
value = 10
}
bar = {
value = 15
}
mt = {
__add = function(left,right)
left.value = left.value + right.value;
return left;
end
}
setmetatable(foo,mt);
foo = foo + bar;
print(foo.value);
prints: 25
But my Question now is which other operators can you overload and if __add is used to access the + operator, how can you access other operators?
Upvotes: 1
Views: 2051
Reputation: 5847
which other operators can you overload
Complete list of metamethods is described in Lua manuals:
http://www.lua.org/manual/5.1/manual.html#2.8
http://www.lua.org/manual/5.2/manual.html#2.4
http://www.lua.org/manual/5.3/manual.html#2.4
if __add is used to access the + operator, how can you access other operators?
See the manual. Metamethods' description tells which operator triggers that exact metamethod.
Upvotes: 4