Reading http://lua-users.org/wiki/LuaClassesWithMetatable the Vector example uses the following technique for the metatable:
Vector={}
Vector_mt={__index=Vector}
function Vector:new(x,y)
return setmetatable({x=x,y=y},Vector_mt)
end
function Vector:add(v)
return Vector:new(self.x+v.x,self.y+v.y)
end
Supposing we want to use __add to support the + operator, we need to explicitly mention it in the metatable, and also we need to reorder things, so that the metatable and the constructor are mentioned after Vector:add:
Vector={}
function Vector:add(v)
return Vector:new(self.x+v.x,self.y+v.y)
end
Vector_mt={__index=Vector,__add=Vector.add}
function Vector:new(x,y)
return setmetatable({x=x,y=y},Vector_mt)
end
To avoiding having to mention each metamethod in the metatable, I can set Vector itself as the metatable, then I can add __add (plus, obviously, __index) as a method of Vector:
Vector={}
function Vector:new(x,y)
return setmetatable({x=x,y=y},Vector)
end
function Vector:add(v)
return Vector:new(self.x+v.x,self.y+v.y)
end
function Vector:__index(k)
return Vector[k]
end
function Vector:__add(b)
return self:add(b)
end
is the latter not recommended and why?