Page 2 of 2

Re: Question - super on middleclass

Posted: Tue Jan 25, 2011 11:12 am
by Robin
BlackBulletIV wrote:The only problem with using SuperClass.method is that it's hard coded to the super class' name. Of course if you changed the super class, then you could easily do a find/replace, and using methods from a super class is somewhat hard coded anyway. But if hard coding the name was a problem, you could use self.super (like you were suggesting before kikito), however I'm not really sure about that syntax (doesn't sit well).
You'll have to explicitly state the superclass anyway (when subclassing), so you'll have to do a search/replace every time you renamed a superclass anyway.

Re: Question - super on middleclass

Posted: Tue Jan 25, 2011 12:53 pm
by leiradel
kikito wrote:There are mixins.
Yeah, I thought I could implement some things I had in mind using mixins, but when I started to write the code I realized they couldn't do what I wanted (please correct me if I'm wrong). In your example, how could I have different implementations of "fly" for different animals?

I could do what I wanted with interfaces if I was using Java. I ended up expanding an old OO implementation for Lua I had lying around with abstract methods so I could do:

Code: Select all

HasWings = class() -- class names are looked up for in the global table.

HasWings.fly = abstract() -- if an abstract method is called it raises a runtime error with both the class and the method name.

Bee = class( Insect, HasWings ) -- MI, but HasWings is more like a Java interface

function Bee:fly() -- implement the "interface" method
  print( 'Bzzz Bzzz Bzzz I am a ' .. self:getClassName() )
end
kikito wrote:it's not possible to do multiple inheritance with middleclass; (snip) I doubt it will change.
Sad, I started to use middleclass because I think everyone should be using the same OO implementation when coding for LÖVE to improve interoperability between snippets/code/modules and it seems middleclass is the most popular but I had to switch to another one that is a better fit to my problems (or would it be mindset?)

Cheers,

Andre

Re: Question - super on middleclass

Posted: Tue Jan 25, 2011 1:20 pm
by kikito
leiradel wrote: Yeah, I thought I could implement some things I had in mind using mixins, but when I started to write the code I realized they couldn't do what I wanted (please correct me if I'm wrong). In your example, how could I have different implementations of "fly" for different animals?
Mixins are like interfaces, but better; they come "filled in" with a "default implementation". But you can still change them.

Your code with mixins would look like this.

Code: Select all

Bee = class( Insect ):includes(HasWings) -- This adds a default fly implementation to Bee

function Bee:fly() -- this re-defines fly for Bee
  print( 'Bzzz Bzzz Bzzz I am a ' .. self:getClassName() )
end
On this particular case, we're redefining the mixin completely, since it only has one function. It is still useful to include it, since includes(HasWings, Bee) will return true; this allows making things a bit more semantic later on (if includes(HasWings, class) then ... ).

If you need more help, let's open another thread, so this one stays on topic.
better fit to my problems (or would it be mindset?)
It's definitively a mindset thing. Coming from Java, you are naturally used to classes being "closed down" and things having to be declared "in advance" (as with interfaces).

Lua's different: No compile-time type checking, & a lots of things done on runtime. It requires a little effort to get used to it, but once you get over the "I'm going to get lots of errors!" feeling, you will realize it is actually very pleasant to use.

Lua doesn't "protect you from yourself" (some people would call that treating you like an adult) but that's an advantage.

Re: Question - super on middleclass

Posted: Tue Jan 25, 2011 1:26 pm
by kikito
BlackBulletIV wrote:The only problem with using SuperClass.method is that it's hard coded to the super class' name. Of course if you changed the super class, then you could easily do a find/replace, and using methods from a super class is somewhat hard coded anyway. But if hard coding the name was a problem, you could use self.super (like you were suggesting before kikito), however I'm not really sure about that syntax (doesn't sit well).
I realized some days ago the self.super boat would not float well.

You see, 'super' changes depending on what method you are on. It's independent from the instance you are sending to it.

When I realized this, I started looking for a way to 'keep track' of the 'current function', but that's only possible with debug.getinfo AFAIK.

Re: Question - super on middleclass

Posted: Tue Jan 25, 2011 5:11 pm
by TechnoCat
kikito wrote:When I realized this, I started looking for a way to 'keep track' of the 'current function', but that's only possible with debug.getinfo AFAIK.
I'm currently using seLOVE and debug doesn't exist.

Re: Question - super on middleclass

Posted: Tue Jan 25, 2011 5:58 pm
by kikito
TechnoCat wrote:I'm currently using seLOVE and debug doesn't exist.
So, that definitively rules out #2. Thanks for commenting!

Re: Question - super on middleclass

Posted: Tue Jan 25, 2011 8:23 pm
by BlackBulletIV
Robin wrote:
BlackBulletIV wrote:The only problem with using SuperClass.method is that it's hard coded to the super class' name. Of course if you changed the super class, then you could easily do a find/replace, and using methods from a super class is somewhat hard coded anyway. But if hard coding the name was a problem, you could use self.super (like you were suggesting before kikito), however I'm not really sure about that syntax (doesn't sit well).
You'll have to explicitly state the superclass anyway (when subclassing), so you'll have to do a search/replace every time you renamed a superclass anyway.
No I actually meant changing the super class of the sub class, not changing the super class' name.
kikito wrote:the "I'm going to get lots of errors!" feeling
Ha ha, I get that all the time... especially with C++.

Re: Question - super on middleclass

Posted: Tue Feb 01, 2011 8:22 am
by kikito
I just finished removing super from middleclass and middleclass-extras.

From now on, you will have to use the Python syntax for invoking superclass methods.

Thanks a lot for the feedback!