There’s so much hate around the usage of metatables, especially for OOP.
Usually people use the __index for their class constructors in lua,
Here is a person class example:
---------[Module]---------
local Person = {}
Person.__index = Person
function Person.new(Name: string)
local object = {}
object.age = 0
object.Name = Name
return setmetatable(Person, object)
end
function Person:AgeUp()
self.age += 1
end
---------[Script]---------
local PersonClass = require(--[[Path]])
local Bob = PersonClass.new("Bob")
Bob = PersonClass:AgeUp()
And here is the proposed solution to luau classes that many people have because for some reason using __index is inefficient?
Functional
Even slietnick supports this: https://www.youtube.com/watch?v=-E_L6-Yo8yQ (he provides no reason why metatables are bad in this video btw)
---------[Module]---------
local Person = {}
function Person.new(Name: string)
local object = {}
object.age = 0
object.Name = Name
return object
end
function Person.AgeUp(Object)
Object.age += 1
end
---------[Script]---------
local PersonClass = require(--[[Path]])
local Bob = PersonClass.new("Bob")
PersonClass.AgeUp(Bob)
The difference you tell me I guess.
I have spoken to these “metatable OOP haters” and they’ve all said that __index is less efficient as it uses more memory…
__index does not create a new table, it just fires whenever you try to index a nil index in the table as you all know.
So like… what’s all the hate for?