Typed Vector.<T> construction

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Typed Vector.<T> construction

Janosch Scharlipp-4
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
_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Typed Vector.<T> construction

Jeff Dyer



On 6/30/08 4:26 AM, Janosch Scharlipp wrote:

> 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()};

Proposed ES4 supports the syntax:

  [new T(), new T] : Vector.<T>

to mean:

  let (TMP=new Vector.<T>) (TMP[0] = new T(), TMP[1] = new T(), TMP)

This gives you the type checking you are looking for, I think.

Array initialiser annotations are not supported in AS for FP10, however, so
you'll have to continue setting elements explicitly to get static type
checking.

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

So that expressions such as the following have clear and unambiguous
meaning:

    new x < y > ( 10 )

Relational comparison of 'new x', 'y' and 10; Or, constructing and instance
of 'x<y>'. Unlike with other languages we can't distinguish between
constructable and comparable expressions (statically), or even values
(dynamically). So we resort to special syntax.

Jd

_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss