local class_mt = {
__name = "class";
}
function class_mt:__call(...)
local obj = setmetatable({}, self)
local init = self.__init
if init then
init(obj, ...)
end
return obj
end
function class_mt:__tostring()
return 'class ' .. self.__name
end
local function obj__tostring(self)
return "instance of " .. self.__name
end
return function(name, ...)
assert(type(name) == 'string', 'Invalid class name')
local class = setmetatable({
__tostring = obj__tostring;
}, class_mt)
for _, base in ipairs({...}) do
for k, v in next, base do
rawset(class, k, v)
end
end
-- We want to override from base classes
rawset(class, "__name", name)
rawset(class, "__index", class)
return class
end