Super
super is a special keyword that can be used in two different ways: It can be treated as an object, or it can be called like a function. It only has special functionality when inside a class.
When called as a function, it will call the function of the same name in the parent class. The current self will automatically be passed as the first argument. (As seen in the inheritance example above)
When super is used as a normal value, it is a reference to the parent class object.
It can be accessed like any of object in order to retrieve values in the parent class that might have been shadowed by the child class.
When the \ calling operator is used with super, self is inserted as the first argument instead of the value of super itself. When using . to retrieve a function, the raw function is returned.
A few examples of using super in different ways: MoonScript:
class MyClass extends ParentClass a_method: => -- the following have the same effect: super "hello", "world" super_method "hello", "world" super.a_method self, "hello", "world"
-- super as a value is equal to the parent class: assert super == ParentClass ```
Lua
local MyClass do local _parent_0 = ParentClass local _base_0 = { a_method = function(self) _parent_0.a_method(self, "hello", "world") _parent_0.a_method(self, "hello", "world") _parent_0.a_method(self, "hello", "world") return assert(_parent_0 == ParentClass) end } _base_0.index = _base_0 setmetatable(_base_0, _parent_0.base) local _class_0 = setmetatable({ init = function(self, ...) return _parent_0.init(self, ...) end, base = _base_0, name = "MyClass", parent = _parent_0 }, { index = function(cls, name) local val = rawget(_base_0, name) if val == nil then return _parent_0[name] else return val end end, call = function(cls, ...) local _self_0 = setmetatable({}, _base_0) cls.init(_self_0, ...) return _self_0 end }) _base_0.class = _class_0 if _parent_0.inherited then _parent_0.__inherited(_parent_0, _class_0) end MyClass = _class_0 end
super can also be used on left side of a Function Stub. The only major difference is that instead of the resulting function being bound to the value of super, it is bound to self.