Repository navigation
Expand file tree
/
Copy pathsubclass.lua
More file actions
189 lines (174 loc) · 5.89 KB
/
Copy pathsubclass.lua
File metadata and controls
189 lines (174 loc) · 5.89 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
--- Class factory.
--
-- **Source**
--
-- Source code can be found [here](https://github.com/LRDPRDX/lua-class).
--
-- **User's guide**
--
-- * a _class table_, or a _class_ - is a table that defines a class. This table can be called
-- to create instances of that class. Returned by the `Class:subclass` method.
-- * an _instance table_, or an _instance_, is a table that represents an instance of a class
-- * the `Class:new` method is the _constructor_ of the class. Called when an instance
-- is created
-- * _class.super_ or _instance.super_ refers to the superclass table (if any)
--
-- To create an instance of class `A` you _call_ `A` with a table as the only parameter.
-- This table
-- is accessible then inside the `A:new` method as `self`:
--
-- local A = Class:subclass('A')
-- function A:new()
-- print(('From A:new() : self.foo = %s'):format(self.foo))
-- end
--
-- local a = A { foo = 'bar' }
-- -- > From A:new() : self.foo = bar
--
-- When an instance is created _all_ constructors in the hierarchy are called
-- in the order from parent to child:
--
-- local A = Class:subclass('A')
-- function A:new()
-- print('A:new()')
-- end
--
-- local B = A:subclass('B')
-- function B:new()
-- print('B:new()')
-- end
--
-- local C = B:subclass('C')
-- function C:new()
-- print('C:new()')
-- end
--
-- local c = C { foo = 'bar' }
-- -- > A:new()
-- -- > B:new()
-- -- > C:new()
-- @module subclass
-- @warning I have to use another name for this module (instead of obvious Class) because of this:
-- https://github.com/lunarmodules/ldoc/issues/249
--- Represents a _class_.
-- @type Class
local Class = {}
--- Creates a subclass of a class.
-- @param name Must be a string. This is the name of new class.
-- @return New class (a table).
--
-- @usage
-- local Class = require('subclass') -- this module
--
-- local Animal = Class:subclass('Animal') -- a class, or a class table
-- local Dog = Animal:subclass('Dog') -- also a class
-- local bobik = Dog { name = 'bob' } -- an instance of the Dog class
function Class:subclass (name)
assert(type(name) == 'string',
('bad argument #1 to subclass (string expected, got %s)'):format(type(name)))
local mt = {
__call = function (class, instance)
instance = instance or {}
assert(type(instance) == 'table',
('bad constructor argument (table expected, got %s)'):format(type(instance)))
instance = setmetatable(instance or {}, class)
local constructors = {}
local current = class
local index = 0
-- Collect all constructors in the hierarchy
while current do
if current.new and not constructors[current.new] then
index = index + 1
constructors[current.new] = index
constructors[index] = current.new
end
current = current.super
end
-- Call constructors in the reverse order (downwards in the hierarchy)
for i = #constructors, 1, -1 do
constructors[i](instance)
end
return instance
end,
__index = self, -- parent class
__name = name,
}
local class = {}
class.__index = class
class.super = self
return setmetatable(class, mt)
end
--- Constructor.
-- Called when an instance of a class is created. The instance created is accessible inside
-- this method as `self`. **Note**: there is no need to return anything from that method.
-- @param _ The `self` parameter, the instance itself. **Note**: the dot syntax here instead of
-- familiar colon one because of two reasons: a) I want to explicitely document the `self`
-- parameter and b) the linter complains about not used parameter if `:new()` is used.
--
-- @usage
-- local Animal = Class:subclass('Animal')
--
-- function Animal:new()
-- print('Animal:new()')
-- self.hp = self.hp or 100
-- self.alive = true
-- end
--
-- local Dog = Animal:subclass('Dog')
--
-- function Dog:new()
-- print('Dog:new()')
-- self.hasTail = true
-- end
--
-- local bobik = Dog { name = 'bob', hp = 200 }
-- -- > Animal:new()
-- -- > Dog:new()
--
-- assert(bobik.alive == true)
-- assert(bobik.hasTail == true)
-- assert(bobik.hp == 200)
-- assert(bobik.name == 'bob')
function Class.new(_)
end
--- Checks whether a given class is a subclass of another one.
-- **Note**: `self` here must be a class - not an instance. To check whether an instance is
-- a type of a class use `Class:isA`.
-- @param other A class table
-- @return `true` if a class is a subclass of another class. `false` otherwise.
-- @usage
-- local Animal = Class:subclass('Animal')
-- local Dog = Animal:subclass('Dog')
-- local Cat = Animal:subclass('Cat')
--
-- Dog:isSubclassOf(Animal) --> true
-- Cat:isSubclassOf(Animal) --> true
-- Cat:isSubclassOf(Dog) --> false
function Class:isSubclassOf (other)
if self == other then
return true
end
local super = rawget(self, 'super')
if super then
return super:isSubclassOf(other)
end
return false
end
--- Checks whether a given object is an instance of a class.
-- **Note**: `self` here must be an instance - not a class. To check whether a class is a subclass
-- of another one use `isSubclassOf`.
-- @param other A class table
-- @return `true` if the object is an instance of a given class. `false` otherwise.
-- @usage
-- local Animal = Class:subclass('Animal')
-- local Dog = Animal:subclass('Dog')
--
-- local bobik = Dog { name = 'bob' }
--
-- bobik:isA(Animal) --> true
-- bobik:isA(Dog) --> true
function Class:isA (other)
local mt = getmetatable(self)
return mt:isSubclassOf(other)
end
return Class