Create an extended type from an existing type

BlitzMax Forums/BlitzMax Programming/Create an extended type from an existing type

JoshK(Posted 2007) [#1]
Type a
EndType

Type b Extends a
EndType

Let's say I have an object of type a. I want to create a new object of type b from it. Is this possible?


Damien Sturdy(Posted 2007) [#2]
You can have:

Type A
 Method CasttoB:B()
  'create new object
 End method
End type



Sadly i've not had any success doing the following. It looks like max can't cast custom types...

Type A
 Method CasttoB:B()
  Obj:B=B(Self)
  Return Obj
 End method
End type



H&K(Posted 2007) [#3]
http://www.blitzmax.com/Community/posts.php?topic=60010#669591

Long topic, but I edited the first post to reflect the solution

The basic answer is as long as the NEW isnt inside the function you use to allocate the values for the first object, than yes. What I tend to do now, is have a "Set" method and a "create" function, then the extended type calls its own create, which calls both sets.
Type TRGB                               'A Colour Tri
'				--------------------------------------------------------------------
	Field F_Red	:Int
	Field F_Green	:Int
	Field F_Blue	:Int
'				--------------------------------------------------------------------
	Method RM_Set:TRgb(P_Red:Int=0,P_Green:Int=0,P_Blue:Int=0)
	
		Self.F_Red	= P_Red
		Self.F_Green    = P_Green
		Self.F_Blue	= P_Blue
		Return Self
	
	End Method
'				--------------------------------------------------------------------
	Function RF_Create:TRgb(P_Red:Int=0,P_Green:Int=0,P_Blue:Int=0)
	
		Return New TRgb.RM_Set (P_Red,P_Green,P_Blue)
	
	End Function
'				--------------------------------------------------------------------
	Method RM_CreateCopy:TRgb()

		Return TRgb.RF_Create (Self.F_Red,Self.F_Green,Self.F_Blue)
	
	End Method
  Etc
EndType
Now if I extned RGB I only have to override the RF_Create, then from the new RF_Create calls the new Set, and the super Set. (Sorry about the syntax, but I didnt think you would would mind)


LAB[au](Posted 2007) [#4]
Sorry I don't get it, how type b is created from type a in the above example? I don't even see any reference to type b.

I have been reading the linked post but this is really a very confusing thread! :)


H&K(Posted 2007) [#5]
I don't even see any reference to type b
Thats cos there isnt any ;) It how to prepare the ground.

All its showing is that you need to keep the place you create the NEW instance seperate from the place you allocate its field values. Thatway you have a method you can call from any extended type.


LAB[au](Posted 2007) [#6]
Got it! Thanks


JoshK(Posted 2007) [#7]
No, the point was that I don't want to copy all the data.

It's not that big of a deal, I was just wondering.


H&K(Posted 2007) [#8]
You dont have to copy all the data, cos you now have self.set and super.set
What you cannot do is overload anyof them.
I admit that as the first impuse is to create and alocate in the same Function, then often it needs a bit of work in the base type.