Arrays are a fundamental part of any programming language, and can have any type of object stored in them, accessed by index. However, generally speaking, objects of different types are not mixed within the same array. This means you can be sure of the type of ALL the objects in a given array, and is where strongly typed arrays come in handy.
Flash is a great platform, but actionscript lacks some of the more ‘complex’ language features that other languages (for example C#) boast. One of these features is generics. Generics is a powerful feature that allow collections of strongly typed objects.
Despite not having true generics, actionscript 3 and Flex allow typed arrays through one of two techniques:
Technique 1: create an array as normal and add the ArrayElementType metadata before the array (or arraycollection) declaration. This defines the allowed data type for each element in the array, and improves performance as the compiler knows the type to expect. This method doesn’t however give any added benefit to the developer – no intellisense in Flex Builder or typed objects returned when accessing an item by index.
Technique 2: Use the new Vector data type for flash player 10. A vector is basically an array whose elements all have the same type. This checks at run time that the inserted items are the correct type, and gives performance benefits. There are problems though – the intellisense in Flex Builder just doesn’t work for Vector types and a vector can’t be assigned as a dataProvider for a repeater. There is also a bug where if you don’t import the base type used for the Vector, then the compiler reports that Vector can’t be found and not the Vector’s based type. Took me a while to figure out this error. I blogged about this problem here and it was reported as a bug on Adobe here
So, there are 2 ways to force strongly typed arrays in actionscript, neither of which are perfect. Fingers crossed Adobe introduce some proper generics with Actionscript 4.