Typed Vector.<T> construction

Janosch Scharlipp ecma4 at lists.scharlipp.de
Mon Jun 30 04:26:39 PDT 2008


Hello,
i am currently programming a lot in ActionScript for Flash Player 10, 
which supports for the first time Vectors.
What annoys me is, that it is not possible to create vectors with 
initial elements AND compile time typing:
In ActionScript you can create a Vector in two ways for example with two 
initial elements:

1)
myVector = Vector.<MyType>([new MyType(), new MyType()]);

This way is very compact, but it does not allow the compiler to check 
types, since [...] is a normal, untyped array.

myVector = Vector.<MyType>([new MyType(), new Object()]);

compiles fine, even if it is definitely wrong. Of course, this is kind 
of an embarrasing error to make as a programmer, but it can happen,
for example when the inheritation chain gets changed, then you get 
runtime errors, even if it could be detected easily by the compiler.


2)
myVector = new Vector.<MyType>();
myVector.push(new MyType());
myVector.push(new MyType());

This enables compile time type checking, but it is not compact and does 
not express the "i want a vector with the following initial elements" 
intention of the programmer.

Is a new syntax for this imaginable?
Other programming languages use something like
foo = new Bar<T>{new T(), new T()};

Besides, why is there this "." between "Vector" and "<MyType>"?

best regards,
Janosch



More information about the Es4-discuss mailing list