Cast from object array to object and then back

BlitzMax Forums/BlitzMax Programming/Cast from object array to object and then back

Warpy(Posted 2009) [#1]
This is a bit esoteric, but it intrigued me:
Local strings$[]=["hi","yo"]
Local arr:Object[]
arr=Object[](strings)
If arr Print "1"
o:Object=strings
arr=Object[](o)
If arr Print "2"


So I start with an array of strings. If I cast that to an object array, that works fine.

If I cast it to an Object, then cast that Object to an Object array, it doesn't work.


The reason I want to do this is, I want to make a kind of generic object enumerator that can iterate over any collection, and I'd like to just pass in an Object to it when I make it.


Sledge(Posted 2009) [#2]
Change...
o:Object=strings
to...
Local o:Object[]= strings
and you get the output you're after.


N(Posted 2009) [#3]
Seems casting from object to an array that is a superclass of the array type doesn't work. String[](o) works, however.

Could've sworn this worked previously..


Warpy(Posted 2009) [#4]
Glad I'm not talking gibberish, Noel. So is it likely to be a bug? To be honest, I won't bother mark if it is, because I can't imagine seriously needing to do this.


N(Posted 2009) [#5]
I don't think it's really a bug, just a sort of odd case. I'm thinking you might be able to modify ObjectDowncast (blitz_object.c) to handle arrays this way, but I'm not really sure since it should already work if it were using that to cast from an object to an array.


Otus(Posted 2009) [#6]
A quick look at blitz_array.c suggests that bbArrayCastFromObject compares type names as strings, whereas bbObjectDowncast compares type pointers. There doesn't seem to be a trivial fix.

Since Object[](String[](o)) works, it'd be consistent for Object[](o) to work too.