Enhance class definition: add inherit method to support base class inheritance

This commit is contained in:
Chipperfluff 2026-01-01 17:20:14 +01:00
parent b32a9d8786
commit 45a226cb7b

View File

@ -70,6 +70,16 @@ function oop.class(def, base)
function cls.setattr(target, key, value) function cls.setattr(target, key, value)
return oop.setattr(target, key, value) return oop.setattr(target, key, value)
end 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, { setmetatable(cls, {
__index = base, __index = base,
@ -85,6 +95,9 @@ function oop.class(def, base)
elseif def ~= nil then elseif def ~= nil then
error("class definition must be a function or name string") error("class definition must be a function or name string")
end end
if base then
cls.inherit(base)
end
return cls return cls
end end