Typed Arrays - Hurray!

Posted: 22/07/08

An image of Typed Arrays - Hurray!

I for one am very pleased to see that the typed Array is making a debut in the forthcoming Flash Player 10 release! For those that have been playing heavily with the Flex API, you know it's "kind of" been possible to type an Array using the following meta-data:

[ArrayElementType("Number")]

private var myArray:Array = new Array();

Unfortunately, this code only works when nested inside <mx:Script> blocks in an MXML application; it doesn't work for pure AS3.0 applications. So Flash has been left behind with this one, and it really emphasises that typed Arrays should have been part of the core player.

In the new Flash Player 10 API, the new Vector class brings the ability to strongly type Arrays, and forces errors when invalid types are pushed onto it. It's worth noting that base types must match exactly - attempting to type the Array to an interface or abstract class, then trying to push derived objects will throw errors.

This will be fine:

var v:Vector.< Sprite > = new Vector.< Sprite >();

This will throw an error:

var v:Vector.< DisplayObject > = new Vector.< Sprite >();

I for one can't wait for this feature to be introduced, as it will remove one of the last weaknesses in enforcing strongly-typed OOP in Flash applications.

Flash Player 10 beta and supporting docs

Keywords for this post: Flash Player 10, ActionScript 3, Array, typed, Flex, Flash