Is this code legit in Lua?
local obj = {}
obj.new = function()
end
return obj
Is this code legit in Lua?
local obj = {}
obj.new = function()
end
return obj
Yes, it is completely legit. However, you may find it productive to simply return the constructor function unless you have a good reason for needing a table where the only member is a 'new' function.
You can do so.
local obj = {}
obj.__index = obj
obj.new = function(val)
local newobj = {}
setmetatable(newobj,obj)
newobj.val = val
return newobj
end
Now, you can create an object of that class.
b = obj.new("string")
Finally, print out the key called val.
print(b.val)
string
This discussion is preserved as it was. New answers, comments, and votes are closed.
1 comment
Thanks a lit! Now I can make new APIs for my models of the game