Modified bbArrayCastFromObject

BlitzMax Forums/BlitzMax Module Tweaks/Modified bbArrayCastFromObject

N(Posted 2009) [#1]
This should help with situations like the one Warpy brought up in this post.

Previously, you could not cast from an Object to an Object[] if the array type was String (or if it was an array of arrays, incidentally). However, both strings and arrays subclass Object, so it's a bit nonsensical that you couldn't cast an object for an array/String[] to an Object[]. So, with one extra line added, you can do that now. It shouldn't cause any errors with existing code either, unless you relied upon the inability to cast from an Object that's a String[] to an Object[].
BBArray *bbArrayCastFromObject( BBObject *o,const char *type ){
	BBArray *arr=(BBArray*)o;
	if( arr==&bbEmptyArray ) return arr;
	if( arr->clas!=&bbArrayClass ) return (BBArray*)BBNULL;
	if( arr->type[0]==':' && type[0]==':' ) return arr;
	if ( (arr->type[0]=='$' || arr->type[0]=='[' ) && strcmp(type,":Object")==0 ) return arr;
	if( strcmp( arr->type,type ) && strcmp( arr->type,":Object" ) ) return &bbEmptyArray;
	return arr;
}

This doesn't make casting work between all types of arrays, but at least it resolves one issue that's probably a tiny bit more common than the others.