I was thinking about how to hide the ability to call methods using dot notation and found a way. I wanted to ask, is this code by any chance an ‘embodiment of stupidity,’ because in my local chat with my scripter friends, they called me an idiot. Look:
--!strict
local MyClass = {}
local MyClassMeta = {}
MyClassMeta.__index = MyClass
local function new(num)
local self = setmetatable({}, MyClassMeta)
self.Value = num
return self
end
function MyClass:GetValue()
return self.Value
end
return {
new = new,
Class = MyClass
}
--By the way, yes, this method works.
Nope. This is totally fine. In fact, it helps prevent another quirk: the ability to call the class’ constructor from an instance of the class, which also means chain-calling:
local instance = Class.new().new().new().new()
Keeping the class separated from the metatable that binds it to instances of that class cleans things up in general. I wouldn’t say you should justify this as a means to avoid auto-completion when indexing with the dot operator though. There are times where it’s convenient to do that
local MyClass = {}
local MyClassMeta = {}
MyClassMeta.__index = MyClassMeta
local function new(num)
local self = setmetatable({}, MyClassMeta)
self.Value = num
return self
end
function MyClassMeta:GetValue()
return self.Value
end
return {
new = new,
Class = MyClass
}
--By the way, yes, this method works.