How can I have an array of several unique types?

BlitzMax Forums/BlitzMax Programming/How can I have an array of several unique types?

ImaginaryHuman(Posted 2005) [#1]
Hi folks. I have a scenario in my code where it looks like I may need to use a linked list *shock horror!*. I would, however, prefer to use an array. I can manage the adding and removal of items from the array myself. But what I want to do is be able to use the array to reference instances of several different object types.

Is it possible to make an array that can store any of several different types?

e.g

Type mytype1
   a:int
End Type
Type mytype2
   b#:float
End Type
Type mytype3
   c:String
End Type
Type mytype4
   d:Int[]
End Type

Local myobjectarray=varioustypes[20]
myobjectarray[0]=New mytype3
myobjectarray[1]=New mytype4
myobjectarray[2]=New mytype1
myobjectarray[3]=New mytype2

Obviously the code to create the array the type instances there is bogus, but it sums up what I need to do. Of course BlitzMax won't accept this as is.

I know that each Type probably has a pointer to it - like an Int Ptr - but Blitz likes to know what type of object the pointer is pointing to - ie a mytype2 Ptr or a mytype4 Ptr. I thought perhaps I could get the pointer to a given type instance and force it into the array as an Int Ptr - and use an array of Int Ptr's?
But then it kind of has to be forced back out again. By forced I mean, type casting and all that or even having to read and write to the array memory directly with PokeInt and PeekInt.

How can I do this? Is there any way? At this time I don't want to resort to a linked list.

I really appreciate your feedback and suggestions.
Thanks


Michael Reitzenstein(Posted 2005) [#2]
Local MyObjectArray:Object[] = New Object[ 20 ]

MyObjectArray[ 0 ] = Object( New MyType1 )
MyObjectArray[ 1 ] = Object( New MyType2 )
MyObjectArray[ 2 ] = Object( New MyType3 )
MyObjectArray[ 3 ] = Object( New MyType4 )

If MyType1( MyObjectArray[ 0 ] )

Local obj:MyType1 = MyType1( MyObjectArray[ 0 ] )

EndIf


FlameDuck(Posted 2005) [#3]
Is it possible to make an array that can store any of several different types?
Yes, but you will probably have to downcast them when you want them out.


Michael Reitzenstein(Posted 2005) [#4]
I'd have thought his question was answered?


ImaginaryHuman(Posted 2005) [#5]
Yes, thanks Michael and also Mikkel for the follow-up.

So it looks like all I need to do is use the `object` type and cast the types to/from as I store and retrieve.

Thanks. I actually thought maybe there wasn't a solution, so I'm glad.
Cheers.


ImaginaryHuman(Posted 2005) [#6]
I eventually decided that although casting to/from an Object type is cool and useful (thanks again), I'm going to have several different arrays, one for each type, rather than a single array. Actually it was a design oversight on my part, I needed to have several arrays otherwise all the different objects are randomly mixed together.

:-)


Robert(Posted 2005) [#7]
I don't think you actually need to cast the instances to an Object type when inserting them into the array.

Since Object is a superclass of all of your custom types, no casting is required.

However casting will be required to convert the Object back to its original type.


Michael Reitzenstein(Posted 2005) [#8]
You can implicitly upcast, but I find that explicit casting makes things more clear.


ImaginaryHuman(Posted 2005) [#9]
Hmm ok good to know, thanks Rob. I guess I will be using this after all.


Jim Teeuwen(Posted 2005) [#10]
You can implicitly upcast, but I find that explicit casting makes things more clear.


Clearer, perhaps. But also slower. Boxing and Unboxing doesnt come free of cost :)

If all types share some properties or methods (template) that is the same ina ll types, use a baseclass and have all of them inherit from it. Then use the basetype is the working type when putting stuff into and out of the array. That way, no casting is needed when getting them out of the array.