This isn’t the case, Luau has introduced an optimization on which it ‘caches’ functions if they are the same, a function can look the same and not be the same, so it’s important to understand how that works.
If you take his implementation and get two objects and compare their functions together, it will, infact, return true.
Your ‘test code’ somewhat breaks this optimization, my guess is that it’s because the functions are declared inside a table construct, and not after a table is created.
For example, in this case, it does return true.
local function Class()
local self = {
_number = 0
}
function self:Add(num)
self._number += num
end
return self
end
print(
Class().Add == Class().Add
)
Something to note is that using this method does make the creating of the object a lot slower, and it does use some more memory, which is the memory needed to be allocated for the extra entries for these functions in the table, however, there are some benefits, and one of those is type satefy. It just works better with types, metatable OOP breaks with metatables from what I’ve experienced, and this method did not.
The tutorial isn’t that great, like, the example is ehh, using table.index = function()
is really ugly. Also, they could’ve declared the properties (not the methods) on the table construct it self, etc, but this method in its principle is fine. AFAIK, there’s not really big performance advantages to metatable OOP, at least since well, 2021. (I have not been active basically since then) Most optimizations have to do with indexing, and I believe it’s actually faster for Luau to find the function index this way, not sure.