Why the hate for __index?

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 :person_shrugging: 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?

I’ll start off by saying; I have nothing against the use of __index, or metatables/metamethods in general. I believe they can be quite useful. I also have never heard anyone hate on the use of __index. With that out of the way, if I had to guess what the people you spoke to meant from the the information you have given.

You are correct to say that __index does not create an entirely new table, but instead refers to a pre-existing one. While I don’t agree that it’s fair to claim that use of __index uses more memory, it is technically true.

In both examples you’ve provided, the exact same tables and functions are created, the only difference being __index. So technically speaking, you are adding one additional entry to the Person table, which does add to the memory-usage. While this is absolutely minimal memory-usage, it is still technically speaking worse for memory.

I think the main reason you would see people dislike OOP in LuaU, is because LuaU is typically used alongside functional programming. So when you make use of OOP, you are breaking some conventions that may cause confusion, and can lead to atypical code, which makes it harder to read.

TL;DR: __index does technically increase memory-usage, however it is very miniscule. People may dislike OOP in LuaU, as it is atypical compared to the usual functional programming people are used to.

Edit:
In the future, I recommend you put threads like these in the Development Discussion category, as it would fit better there.

3 Likes