summaryrefslogtreecommitdiff
path: root/class.lua
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2024-01-04 18:57:31 +0000
committerubq323 <ubq323@ubq323.website>2024-01-04 18:57:31 +0000
commit48aa74743b3d7cc73f18b8ef32353629cf8dd6a2 (patch)
tree82d5564fb830136ae54936e6152c800fda90c576 /class.lua
initial
Diffstat (limited to 'class.lua')
-rw-r--r--class.lua38
1 files changed, 38 insertions, 0 deletions
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})
+
+