Redeclaring data members in extended types

BlitzMax Forums/BlitzMax Programming/Redeclaring data members in extended types

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I find it best not to use New(). I instead prefer a Create() function or method. It is a little more work, but you gain a lot more flexibility that way. For example:
SuperStrict

Type A
	Field x:Int

	Method Create:Object(x:Int)
		Self.x = x + 10
		
		Print "In Type A"
		Return Self
	End Method
End Type

Type B Extends A
	Field x:Int = 0
	
	Method Create:Object(x:Int)
		Super.Create(x)
		
		Print "In type B"
		Return Self
	End Method
End Type	

Type C Extends B
	Field x:Int

	Method Create:Object(x:Int)
		
		Super.Create(x)
		Self.x = x
		
		Print "In Type C"
		Return Self
	End Method
End Type

Local MyA:A = A(New A.Create(3))
Print "MyA.A = "+MyA.x
Print

Local MyB:B = B(New B.Create(7))
Print "~nMyB.A = " + A(MyB).x
Print "MyB.B = " + MyB.x
print

Local MyC:C = C(New C.Create(10))

Print "~nMyC.A = "+A(MyC).x
Print "MyC.B = "+B(MyC).x
Print "MyC.C = "+MyC.x

You now can make an instance of any parent or extended type, and pass it parameters. The calling order is from base type to extended type. However, if you move the call to Super.Create() so it is right above the Return Self, then it will be called in extended to base order.
As a matter of fact, by changing where the Super.Create() is called, you can pretty much have the base types called in any order you want at any place in the initialization.

Edit: Just read your reply after I posted. The above method will require the same parameter types be passed to all extended classes, but since I am returning Object and then casting back to the appropriate type at the New command, you can extend this to handle any type.