Super?

BlitzMax Forums/BlitzMax Beginners Area/Super?

Jeroen(Posted 2004) [#1]

Super Super evaluates to Self cast to the method's immediate base class.


What? Hehe, I really, really don't get that :-)


Robert(Posted 2004) [#2]
EDIT: Didn't explain it too well, lets try again:

Supposing we had a type Animal and a type which extends Animal called Cat. If they both implemented different Die methods, then calling Super.Die from inside one of Cat's methods actually calls Animal.Die because Cat is a type of Animal.

Calling Self.Die or just Die on the other hand from one of Cat's methods would call Cat.Die

Type Animal
   Method Die()
       Print "An animal died."
   End Method
End Type

Type Cat Extends Animal
   Method Die()
       Print "A cat died."
   End Method

   Method WaysToKillACat()
      'Prints "A cat died" (same as just Die on its own)
       Self.Die
    
       'Prints "An animal died"
       Super.Die
   End Method
End Type



Algo(Posted 2004) [#3]
So it's like referencing an extended type's parent?

If so, why is it called Super?


Robert(Posted 2004) [#4]
Yes.

I don't know why it is called super, I personally think the term Base would be better, but it doesn't really matter.


Jeroen(Posted 2004) [#5]
Hi Robert,

Good example! Thank you. "super" doesn't sound "Blitz" too me, what is does and how it is named isn't logical to me. Robert's example should appear in the docs!


AaronK(Posted 2004) [#6]
Super is Javaesque, Base is C++ esque - Blitz is more Java like in it's other keyword uses (Final, Abstract) too.


marksibly(Posted 2004) [#7]
Also, I tend to think along the lines of...

Type X
End Type

Type Y Extends X
End Type

Type Z Extends Y
End Type

X and Y are both base types of Z. However, only Y is Z's super type - ie: 'immediate' base type.


AaronK(Posted 2004) [#8]
Ahh, interesting differentiation. So can you get to a non immediate base with Blitzmax?

Aaron


marksibly(Posted 2004) [#9]
Just tried it...and no:

Type X
	Method T()
		Print "X.T"
	End Method
End Type

Type Y Extends X
	Method T()
		Print "Y.T"
	End Method
End Type

Type Z Extends Y
	Method T()
		Super.Super.T
		Print "Z.T"
	End Method
End Type

Local t:Z=New Z

t.T


I probably could get this to work - but I'm not sure if it's a good idea style-wise!


Perturbatio(Posted 2004) [#10]
Could you maybe do it by having a GetSuper method in each type that returns access to it's super?


AaronK(Posted 2004) [#11]
Well you could argue that only the most recent derived class should call the super, but howabout a class reference call instead. So using your example you'd go

Type Z Extends Y
Method T()
X.T <- state X explicitly
Print "Z.T"
End Method
End Type


FlameDuck(Posted 2004) [#12]
I probably could get this to work - but I'm not sure if it's a good idea style-wise!
No. Use composite pattern instead.


Sweenie(Posted 2004) [#13]
What is the purpose of the abstract keyword after the method in the example above?
The docs says that it means this method has no implementation and it must be implemented by a derived type.
So wouldn't Super.Die cause an error or does it work because it is called from a derived type?


AaronK(Posted 2004) [#14]
If I understand correctly, while you must override it, it still has a default implementation - yes, so you get base (Super) class functionality still, even if you really need to derive from the class as a whole.


marksibly(Posted 2004) [#15]

If I understand correctly, while you must override it, it still has a default implementatio



No - abstract methods do not have an implementation. That code will not compile.


AaronK(Posted 2004) [#16]
Ahh OK Thanks. I hadn't tried it, and assumed that that code was compiling and therefore worked like C++.

Aaron


TommyBear(Posted 2004) [#17]

Ahh OK Thanks. I hadn't tried it, and assumed that that code was compiling and therefore worked like C++.



In C++, pure virtual functions are empty and must be implemented by the derived class.


marksibly(Posted 2004) [#18]
Actually, in C++ you *can* provide an implementation for a pure virtual function.


Warren(Posted 2004) [#19]
Unless we're talking about different things, that's incorrect. Virtual functions are optional, but a pure virtual function has to be overloaded in the derived class or the compiler will throw a fit.

virtual MyFunction();

vs

virtual MyFunction() = 0;


Kanati(Posted 2004) [#20]
I'm with Warren on this one... HAS to be overridden by the class that inherits the base class.


skidracer(Posted 2004) [#21]
Not wanting to nit pick, but Mark said implementation not instantiation.


TommyBear(Posted 2004) [#22]

Actually, in C++ you *can* provide an implementation for a pure virtual function.



http://www.parashift.com/c++-faq-lite/abcs.html#faq-22.4

Take that! :p