Hey,
I'm new(ish) to LOVE, I have a bit of background knowedge of object-oriented programming but usually with "higher-level" engines that do a lot of the heavy lifting when it comes to creating/storing/manipulating objects.
I'm currently working on a project in LOVE using rxi's Classic library and I've got the basics of it down, I can create new objects, give properties to those objects, create methods for those objects etc. but I'm a bit stuck with what exactly <Class>.super.new(self, {}) is and what it does.
I know it is something to do with setting (or... getting?) the "super" ("parent class") of an object but I don't understand what the first and second arguments represent or why it is necessary to insert this particular piece of code when creating a new class; for example I had a piece of code that wasn't working until I inserted <Class>.super.new(self, {}) and then it worked no problem, but I don't understand what actually happened and why.
If anyone could explain this to me I'd be really grateful, I've had a quick search of the LOVE forums, Stack Overflow etc. but there's nothing that answered my question in a way I could understand and I'm now more confused than I initially was lol.
Thanks!
[ANSWERED] What does "<Class>.super.new(self, {})" do?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 5
- Joined: Fri Nov 04, 2022 12:44 pm
[ANSWERED] What does "<Class>.super.new(self, {})" do?
Last edited by mr_3tianne on Sat Nov 05, 2022 3:19 pm, edited 1 time in total.
Re: What does "<Class>.super.new(self, {})" do?
This is calling the parent constructor. Because the OOP is just emulated there's no mechanism for Lua to call any methods in the parent class. For example:
will print the following:
It's up to you to forward constructor and method calls to the parent, unlike in Java/C# like OOP languages the default parent constructor is called automatically, unless you want to override it to use a different overloaded constructor. You still have to deliberately call parent methods though. My C++ is a little rusty so I'm not sure how that behaves, I suspect it's similar though.
Edit: Missed the self parameter.
Code: Select all
local ParentClass = Object:extend()
function ParentClass:new()
print("ParentClass constructor")
end
function ParentClass:doSomething()
print("ParentClass doSomething")
end
local ChildClass = ParentClass:extend()
function ChildClass:new()
ChildClass.super.new(self)
print("ChildClass constructor")
end
function ChildClass:doSomething()
ChildClass.super.doSomething(self)
print("ChildClass doSomething")
end
local child = ChildClass()
child:doSomething()
Code: Select all
ParentClass constructor
ChildClass constructor
ParentClass doSomething
ChildClass doSomething
Edit: Missed the self parameter.
-
- Prole
- Posts: 5
- Joined: Fri Nov 04, 2022 12:44 pm
Re: What does "<Class>.super.new(self, {})" do?
Thank you for your explanation, I think I understand what's going on now. So just to confirm, in my example of <Class>.super.new(self, {}) the blank table (or whatever the second argument is) is the argument(s) for constructing the "parent" class?
Re: What does "<Class>.super.new(self, {})" do?
It's the argument passed to the constructor of the parent class. See the README of Classic, there's a very similar example where instead of {}, it uses x, y.
It's used to construct an object; the class of the object will be the class passed as self, but the parent's constructor will initialize it as if it were an object of the parent class (which is possible because they are compatible). The constructor of the child class will then add its own specializations to that base object.
In the example in the README, the constructor of Rect will create an object of class 'self', which will be first initialized as if it were a Point, and then the constructor of Rect will add its own specializations (the width and height, to make it a rectangle instead of a point).
The 'self' that Rect receives may be the Rect class itself, or any class that is a child of Rect, but whatever it is, the constructor will initialize it as if it were a Rect.
It's used to construct an object; the class of the object will be the class passed as self, but the parent's constructor will initialize it as if it were an object of the parent class (which is possible because they are compatible). The constructor of the child class will then add its own specializations to that base object.
In the example in the README, the constructor of Rect will create an object of class 'self', which will be first initialized as if it were a Point, and then the constructor of Rect will add its own specializations (the width and height, to make it a rectangle instead of a point).
The 'self' that Rect receives may be the Rect class itself, or any class that is a child of Rect, but whatever it is, the constructor will initialize it as if it were a Rect.
-
- Prole
- Posts: 5
- Joined: Fri Nov 04, 2022 12:44 pm
Re: What does "<Class>.super.new(self, {})" do?
That makes sense. Thank you!
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 4 guests