diff --git a/oop/oop.lua b/oop/oop.lua index 93fc3c2..2d2c823 100644 --- a/oop/oop.lua +++ b/oop/oop.lua @@ -70,6 +70,16 @@ function oop.class(def, base) function cls.setattr(target, key, value) return oop.setattr(target, key, value) end + function cls.inherit(parent) + cls.__base = parent + cls.super = parent + local mt = getmetatable(cls) or {} + mt.__index = parent + mt.__call = mt.__call or function(c, ...) + return oop.new(c, ...) + end + setmetatable(cls, mt) + end setmetatable(cls, { __index = base, @@ -85,6 +95,9 @@ function oop.class(def, base) elseif def ~= nil then error("class definition must be a function or name string") end + if base then + cls.inherit(base) + end return cls end