From 48aa74743b3d7cc73f18b8ef32353629cf8dd6a2 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 4 Jan 2024 18:57:31 +0000 Subject: initial --- class.lua | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 class.lua (limited to 'class.lua') diff --git a/class.lua b/class.lua new file mode 100644 index 0000000..c9fb87b --- /dev/null +++ b/class.lua @@ -0,0 +1,38 @@ +-- minimal class library +-- +-- local Foo = class() +-- function Foo.make(cls, x,y,z) +-- return setmetatable({x=x,y=y,z=z},cls) +-- end +-- function Foo.whatever(self, ...) +-- ... +-- end +-- ... +-- local foo = Foo:make(...) +-- foo:whatever(...) + + +local function class() + local T = {} + T.__index = T + return T +end + +local function extend(Base) + local T = {} + T.__index = T + for k,v in pairs(Base) do + if k:sub(1,2) == "__" and k~="__index" then + T[k]=v + end + end + setmetatable(T,{__index=Base}) + return T +end + +return setmetatable({ + class=class, + extend=extend +},{__call=class}) + + -- cgit v1.2.3