ngwese
85
FWIW I quickly scanned the tutorialpoint page before responding and I think their tutorial is frankly incorrect. In each new function they define they start with o = o or {} but then they assign all of the would be instance values to the class table. I tend to stick with the official Lua docs at the end of the day despite their shortcomings.
The : vs . syntax is a source confusion in my experience. Hopefully the following sheds some additional light:
Class = {}
function Class:new(val)
o = {}
setmetatable(o, self)
self.__index = self
o.val = val
return o
end
function Class:changeValA(newval)
self.val = newval
end
function Class.changeValB(self, newval)
self.val = newval
end
function Class.changeValC(nothing_special, newval)
nothing_special.val = newval
end
Class.changeValD = function(self, newval)
self.val = newval
end
local obj = Class:new(1)
print(obj.val)
obj:changeValA(2)
print(obj.val)
-- calling a function defined with implicit self parameter via normal function invocation
obj.changeValA(obj, 3)
print(obj.val)
obj:changeValB(4)
print(obj.val)
obj:changeValC(5)
print(obj.val)
obj:changeValD(6)
print(obj.val)
Notice that I’ve defined essentially the same method using three different syntactic styles. Defining a function/method with Class:func is shorthand for assign the func key in the Class table to the following function and implicitly add a self parameter.
The second definition is much like the first but uses . so I add the self parameter explicitly to allow the use of the : “method call” syntactic sugar.
The third changeValC is like the second but illustrates that the name of the first parameter is not special.
The fourth form de-sugars it completely showing that defining “methods” is nothing more than assigning a name within a table to a function value.
8 Likes