Parameters to constructor

BlitzMax Forums/BlitzMax Beginners Area/Parameters to constructor

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.

The kludge feeling would be reduced if you BlitzMax implemented:

a) Method overloading
b) Derived functions that can return compatible derived types

Type T_Place

	Field Name:String

	Function Create:T_Place (name:String)
		Local obj:T_Place = New T_Place
		
		obj.Name = name
		
		Return obj
	End Function

End Type

Type T_City Extends T_Place

	Field Population:Int
	
	Function Create:T_City (name:String, population:Int)
		Local obj:T_City = New T_City
		
		obj.Name = name
		obj.population = population
		
		Return obj
	End Function
	
End Type

This gives a compile error: "Overriding method differs by type".

So the derived type factory has to be called something else like CreateCity and you say:

Local city:T_City = T_City.CreateCity("Sydney", 3276207)

Whereas I'd like to say:

Local city:T_City = new T_City("Sydney", 3276207)

Also the derived type has no built-in way to pass parameters to its base type constructor so you end up having to implement yet another differently named method in each type for its descendents to set its parameters

' Star type - These guys shine and occasionally explode
Type T_Star Extends T_Natural

	Function CreateStar:T_Star(name:String, pos:T_Pos, vel:T_Pos, colour:T_Colour, mass:Float, radius:Float, rot:Float, rotRate:Float)
		Local obj:T_Star = New T_Star

		obj.SetStarParms(name, pos, vel, colour, mass, radius, rot, rotRate)
		
		Return obj
	End Function

	Method SetStarParms(name:String, pos:T_Pos, vel:T_Pos, colour:T_Colour, mass:Float, radius:Float, rot:Float, rotRate:Float)
	
		super.SetNaturalParms(name, pos, vel, colour, mass, radius, rot, rotRate)
	
	End Method
	
End Type

Here T_Star is derived from T_Natural and happens to have the same parameters although it could have additional ones or even not have some but provide defaults. I regard it as duplication for T_Star to directly set the properties that it inherits from T_Natural. It should be possible to pass these to the super constructor in a minimal way.

I'd like to see something like the following in BlitzMax (based on C# syntax):
' Star type - These guys shine and occasionally explode
Type T_Star Extends T_Natural

	Method New(name:String, pos:T_Pos, vel:T_Pos, colour:T_Colour, mass:Float, radius:Float, rot:Float, rotRate:Float)
	: base(name, pos, vel, colour, mass, radius, rot, rotRate)
	'  Anything else you want to initialise here
	End Method

End Type

Add in that you might want constructors with various parameters and you start to write things like:

' Default empty constructor
Function CreateStar:T_Star()

' Constructor with Name, Pos, Vel, Colour, Mass
Function CreateStarNPVCM:T_Star(name:string, ...)

' Constructor with everything
Function CreateStarNPVCMRRR:T_Star(name:string, ...)

This is the area in which BlitzMax is weakest as a language with OO aspirations.