-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass.lua
More file actions
35 lines (29 loc) · 923 Bytes
/
Copy pathclass.lua
File metadata and controls
35 lines (29 loc) · 923 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
31
32
33
34
35
--- @module class
-- Utility for working with idiomatic Lua object-oriented patterns
local class = {}
--- Given an `object`, return its class
function class.classOf(object)
return getmetatable(object)
end
--- Given an `object` and a `class`, return if that object is an instance of another class/superclass
-- Equivalent to @{class.extends}(@{class.classOf}(object), `cls`)
function class.instanceOf(object, cls)
return class.extends(class.classOf(object), cls)
end
--- Get the superclass of a class
function class.getSuperclass(theClass)
local meta = getmetatable(theClass)
return meta and meta.__index
end
--- Check if one class extends another class
function class.extends(subclass, superclass)
assert(type(subclass) == "table")
assert(type(superclass) == "table")
local c = subclass
repeat
if c == superclass then return true end
c = class.getSuperclass(c)
until not c
return false
end
return class