From 105d265792e5c5294c91aa2a9d1bb0d4a11bc4b1 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Mon, 11 Mar 2024 14:58:54 +1100 Subject: [PATCH] feat(class): add temporary super method during class init --- imports/class/shared.lua | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/imports/class/shared.lua b/imports/class/shared.lua index 20548ce..e95c700 100644 --- a/imports/class/shared.lua +++ b/imports/class/shared.lua @@ -16,8 +16,8 @@ local function assertType(id, var, expected) if received ~= expected then error( - ("expected %s %s to have type '%s' (received %s)"):format(type(id) == 'string' and 'field' or 'argument', id, - expected, received), 3) + ("expected %s %s to have type '%s' (received %s)"):format(type(id) == 'string' and 'field' or 'argument', id, + expected, received), 3) end return true @@ -34,7 +34,19 @@ function mixins.new(class, obj) setmetatable(obj, class) - if class.init then obj:init() end + if class.init then + local parent = class + + function obj:super(...) + parent = getmetatable(parent) + local superInit = parent and parent.init + + if superInit then return superInit(self, ...) end + end + + obj:init() + obj.super = nil + end return obj end