gitcalculus
5/17/2018 - 9:16 AM

Lua Class

Another lua class


function class(super)
    local new_type = {}
    new_type.__index = new_type
    setmetatable(new_type, {
        __index = super, --this is what make the inheritance work
        __call = function(cls, ...) -- [ClassName](...)
            print(cls.name)
            local new_inst = {}
            setmetatable(new_inst, cls)
            newCtor = cls["ctor"]
            if newCtor then -- if new class has ctor, direct execute this ctor
                new_inst:ctor(...)
            end
            return new_inst
        end
    })
    return new_type
end

function extend(super, ext)
    local new_type = class(super)
    for k,v in pairs(ext) do
        new_type[k] = v
    end
    return new_type
end