Appearance
Classes
Classes are supported by using maps. Therefore there is technically no difference between an object and a class. Inheritance is prototype-based.
gs
MyClass = {
"classID": "test",
}
MyClass.returnAnswer = function
return 42
end function
instance = new MyClass
print(instance.returnAnswer) //prints 42
Classes can get defined by creating a new map. Optionally a classID can be set which is useful when using typeof. By using the new keyword you can spawn a new instance of the defined class. Essentially by calling new a __isa member gets set.
Self
Class methods can make use of the self identifier. The self identifier will expose the currently called instance.
gs
MyClass = {
"classID": "test",
"counter": 0,
}
MyClass.inc = function
self.counter += 1
return self
end function
instance = new MyClass
instance.inc.inc.inc
print(instance.counter) //print 3
Inheritance
Inheritance is handled through the __isa member. To call parent methods the super keyword can be used.
gs
MyClass = {
"classID": "test",
}
MyClass.returnAnswer = function
return 42
end function
MySubClass = new MyClass
MySubClass.classID = "test-sub"
MyClass.returnAnswer = function
return 123
end function
MySubClass.returnOriginalAnswer = function
return super.returnAnswer
end function
instance = new MySubClass
print(instance.returnOriginalAnswer) //prints 42
Isa
The isa keyword can be used to identify instances.
gs
MyClass = { "classID": "test" }
MySubClass = new MyClass
MySubClass.classID = "test-sub"
instance = new MyClass
subInstance = new MySubClass
print(instance isa MyClass) //prints 1
print(instance isa MySubClass) //prints 0
print(subInstance isa MyClass) //prints 1
print(subInstance isa MySubClass) //prints 1