-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
30 lines (26 loc) · 858 Bytes
/
init.lua
File metadata and controls
30 lines (26 loc) · 858 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
return function(parents, methods)
local methods_ = methods or parents
local class_ = methods_
local instance = {}
-- Copy methods from parents to instance and class_
for _, parent in ipairs(parents) do
for name, method in pairs(parent) do
class_[name] = method
end
end
-- Copy methods from methods_ to instance and class_
for name, method in pairs(methods_) do
instance[name] = method
class_[name] = method
end
-- Define the `new` method
function instance:new(...)
local obj = setmetatable({}, {__index = class_})
if class_.init then
class_.init(obj, ...)
end
return obj
end
-- Set the metatable for instance to allow calling as a constructor
return setmetatable(instance, {__call = instance.new})
end