Casting an array of CustomClass into array of Sprite?

I am trying to cast an array of instances of CustomClass ( that extends Sprite ), into array of Sprite.

So, is this the right way :

var instanceArray:Array = new Array<CustomClass>() ;
cast(instanceArray, Array<Sprite>)   ; 

Or, such casting is not possible ?

I am getting error saying Cast type parameters must be Dynamic.

I believe you can directly use the CustomClass because of the inheritance (aka extends Sprite).

var instanceArray:Array<Sprite> = new Array();
//or Literal Syntax
var instanceArray:Array<Sprite> = []; 
//Add to Array
instanceArray.push(new CustomClass());

Looks like the formatting ate your type parameters. To avoid that, put four spaces before each line of code.

I assume you meant this:

var instanceArray:Array<CustomClass> = new Array<CustomClass>();
cast(instanceArray, Array<Sprite>);

The problem with this sort of thing is, declaring an array of type CustomClass is an implicit guarantee that the array will only contain CustomClass objects. In other words, this should always be safe:

var myCustomClassObject:CustomClass = instanceArray[0];

But if you cast it to an array of Sprites, now you can add other things:

cast(instanceArray, Array<Sprite>).push(new MovieClip());
var myCustomClassObject:CustomClass = instanceArray[0];

One of those lines would give you an error (even if the cast itself worked). I’m not sure which one.

But if you really, really don’t care, and Blufedora’s solution doesn’t work, you can do this:

var customClassArray:Array<CustomClass> = new Array<CustomClass>();
var spriteArray:Array<Sprite> = cast customClassArray;

This is what’s known as an “unsafe cast,” and while it isn’t always unsafe, in this case it certainly is. Use with caution.