New() and Abstract Methods

BlitzMax Forums/BlitzMax Programming/New() and Abstract Methods

beanage(Posted 2010) [#1]
I am not sure whether this is a bug report, but its propably more a glitch than a bug.. Consider the following scenario:

Type MyAbstractType Abstract
	Method New()
		DoGeneralStuff
		DoSpecificStuff '!!!!!!!
		'Do more initialization, which only can be done after
		'<DoSpecificStuff> has been invoked!
	End Method

	Method DoGeneralStuff()
		'blahblah
	End Method

	Method DoSpecificStuff() Abstract
End Type

Type MyDerivedType Extends MyAbstractType
	Method DoSpecificStuff()
		'blahblah
	End Method
End Type

Local myObject:myDeriveedType = New myDerivedType


Now, when invoking <DoSpecificStuff> in <New>, you will get an "Attempt to invoke abstract method" error. I understand, that somehow the constructors only operate on the part of the object belonging to their specific type, therefore you cant access fields,methods etc of a derived type in the super constructor. But at this point, the compiler could know that while creating an object of a none-abstract type, when I call the <DoSpecificStuff> abstract template method in the myAbstractType constructor, i mean the one in myDerivedType, couldnt it?


Dreamora(Posted 2010) [#2]
No it can't know it.

your abstract class has no knowledge of any class expanding it as such it can't call anything on them. Its only vice versa, the extended knows what the abstract has.
Also you don't need it in there.

If you want to call it on the constructor, add a Method New() to the extended type which calls DoSpecificStuff for you (new on the abstract will be called to so the base stuff still happens)

you normally declare functions as abstract that other classes need granted to be present on classes that extend your abstract one so they can call it basing on "it is a MyAbstractType so it must have DoSpecificStuff"


beanage(Posted 2010) [#3]
they can call it basing on "it is a MyAbstractType so it must have DoSpecificStuff"


Yea, thats what I expected the compiler to recognize when invoking <DoSpecificStuff> in the constructor of MyAbstractType. Transforming <DoSpecificStuff> to a secondary New in MyDerivedType wont work for me, as the initialization of MyAbstractType already relies on <DoSpecificStuff>.. but.. oh well. Its propably bad design on my part.


plash(Posted 2010) [#4]
Use an initialize method in the base class that does the setup calls, then call that from your extending type's new/create method?


beanage(Posted 2010) [#5]
Use an initialize method in the base class that does the setup calls, then call that from your extending type's new/create method?


Yea.. well.. thats still some violation of DRY, but it seems like an ok workaround.


Wiebo(Posted 2010) [#6]
tip: If you put the specific stuff in your extended type in its own New() method, it will get called. All New() methods are called, from the abstract to the extended, in that order. You can then call DoMoreInitalization() from the same New() method.


Czar Flavius(Posted 2010) [#7]
If you want to do it the way you posted, you could try turning DoSpecificStuff into an empty non-abstract method and see if that changes anything. But the above suggestions are better design.