I cant think of a good name for this question, apologies if this one is long winded or confusing.
I'm finally getting acquainted with OOP, for my current project I'm making a Vector pseudo-class.
Vector = setmetatable({}, {__call = function(_, x, y, z)
x, y, z = x or 0, y or 0, z or 0
return setmetatable({
-- VALUES
["x"] = x,
["y"] = y,
["z"] = z,
-- METHODS
["Add"] = function(augend, addend)
-- code adding the Vectors
end,
}, {
__add = function(augend, addend)
-- code adding the Vectors
end,
})
end,__metatable = true})
return Vector
misc code removed for brevity
Here's the differences between how the two methods would work.
__add
local Vector = require(pseudoVector) -- requiring the above module
local originalVector = Vector(1, 1, 1)
local addendVector = Vector(0, 1, 0)
local newVector = originalVector + addendVector
print("newVector = " .. newVector)
print("originalVector = " .. originalVector)
newVector = 1, 2, 1 originalVector = 1, 1, 1
Add()
local Vector = require(pseudoVector) -- requiring the above module
local originalVector = Vector(1, 1, 1)
local addendVector = Vector(0, 1, 0)
local newVector = originalVector:Add(addendVector)
print("newVector = " .. newVector)
print("originalVector = " .. originalVector)
newVector = 1, 2, 1 originalVector = 1, 2, 1
The __add method adds addendVector to originalVector, puts the result into a new Vector, and returns it. Whereas the Add function adds addendVector directly to originalVector, changing its values.
I'm not too sure how that would work.
Like how would I directly modify the augend's values from a function, which to my knowledge creates a copy of the data instead of directly referencing it. Or how I would use my Metatable inside itself to create a new Vector instance to return.
#Any help would be appreciated.

2 comments
augend is table which means it is not copied and you can edit it's values in the function.
ok that clears things up.