ActionScript 3 Types Typed arrays


Unfortunately ActionScript 3 does not have a concept of generics, so it follows that there is no way to define a typed array as Array<T>. There is however a special class Vector.<T> which works in a similar way except you must provide a reference to a concrete class when instantiating the vector. This means there is no way to build abstractions on top of the Vector.<T> type (e.g. extend it and add new functionality) which is a huge drawback.

A simpler way to look at is is as though every class you define automatically had a companion class named Vector.<NameOfYourClass>.

With that said, there are still huge advantages to the Vector.<T> type over a conventional array:

  • There is a performance increase when working with Vector.<T> vs arrays1.
  • You receive compile-type TypeErrors if you attempt to insert non-T values into the collection.
  • IDEs provide useful type hinting information for the objects inside a Vector.<T> instance.

Examples of creating a Vector.<T>:

var strings:Vector.<String> = new Vector.<String>(); // or
var numbers:Vector.<Number> = new <Number>[];

1 Vectors actually only provide notable performance improvements over arrays when working with primitive types (String, int, uint, Number, etc).