-
Notifications
You must be signed in to change notification settings - Fork 0
/
abc.lua
49 lines (36 loc) · 1.15 KB
/
abc.lua
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
---
-- abc.lua
local class = require "middleclass"
local ABC = class("ABC")
function ABC:initialize()
self.abstractmethod = "__ABSTRACT_METHOD__"
self.abstractmethods = {}
end
function ABC:set_abstract_methods(cls)
for k, v in pairs(cls.__declaredMethods) do
if k ~= "initialize" then
if v(self) == self.abstractmethod then
self.abstractmethods[k] = v
end
end
end
if not next(self.abstractmethods, nil) then
error("No abstract methods found! All abstract methods must return self.abstractmethod")
end
end
function ABC:check_abstract_methods(cls)
local not_implemented_methods = {}
for k, _ in pairs(self.abstractmethods) do
if not cls.__declaredMethods[k] then
table.insert(not_implemented_methods, k)
end
end
if #not_implemented_methods > 0 then
local str = ""
for _, method_name in ipairs(not_implemented_methods) do
str = str .. " " .. method_name .. ","
end
error("Can't instantiate abstract class " .. cls.name .. " with abstract methods [" .. str .. "]")
end
end
return ABC