Hi Indigo Gem,
I have just tried your code. I just made one change: Instead of
Code: Select all
self.super.class.initialize(self, params)
The class and super had to be switched:
Code: Select all
self.class.super.initialize(self, params)
Here's the complete code:
Code: Select all
require 'middleclass'
Test1 = class("Test1")
Test2 = class("Test2", Test1)
Test3 = class("Test3", Test2)
function Test3:initialize(params)
self.class.super.initialize(self, params)
end
local obj = Test3:new()
This worked just fine - it run without giving me any errors or entering any infinite loop.
Is the code that you used here complete, or is there any missing stuff? Notably - are there definitions of Test1.initialize and/or Test2.initialize?
I'm asking because you are using this construction:
If you used self.class.super inside the Test2 initializer, you will get an infinite loop if self is an instance of Test3. For instances of Test3, "self.class" is
always Test3, and "self.class.super" is
always Test2 ...
even on methods inherited from Test2! In consequence, "self.class.super.initialize"
always refers to "Test2.initialize". If you call it from itself, you get an infinite loop.
The only solution is not using "self.class.super", but using the class instead:
If the code you have pasted there is complete (there are no missing constructors, etc) then your version of middleclass is probably not updated. Try again with the latest version.