Should I use metatables?

I am making an OOP module, should I use metatables?
Example of what I mean:

-- metatables
local module = { }
module.__index = module

function module.new()
    local Class = setmetatable({ }, module)
    Class.Instance = "something"
    return Class
end

or should I do:

local module = { }

function module.new()
    local t = { }

    function t:print(...)
        print(...)
    end

    return t
end

return module

I believe they achieve the same thing. If not, tell me!

1 Like

I think it depends on what you want to acheive here.

Personally, I’d use metatables if I wanted to have custom events/functions.

What does that mean? Do you mean custom RBXScriptConnection's?

I tried both, they worked completely the same.

Generally speaking the convention would be using metatables in order that class member functions would be defined outside of the constructor.

Both work, but I personally use metatables when following an OOP design.

2 Likes

You’re also not constructing functions for every object, which helps out with performance, especially when working with large quantities of objects

1 Like

I see where your going and what you mean- If I call the constructor lots of times new functions will be constructed as well.