How should I use __index?

why do you do

local Class = {}
Class.__index = Class

function Class.new()
  return setmetatable({}, Class)
end

return Class

instead of

local Class = {}

function Class.new()
  return setmetatable({}, { __index = Class })
end

return Class

No real reason to do the second, you’re just creating an unnecessary extra metatable every time you make an instance of that class. There’s also some metamethods that rely on the two tables having the same metatable if I remember right. (__eq?)

3 Likes

It’s just a way to not create a new table everytime the class is initiated