Type parameters

Nicolas Cannasse ncannasse at motion-twin.com
Tue Sep 5 00:05:23 PDT 2006


> 2. I am also wondering about the implications of not being able to
> enforce any constraints on the types that may be used a parameter. But
> I'm struggling to find a convincingly realistic example where it would
> be useful - so perhaps I will leave that alone...

There are a lot of useful cases. Basically when you don't have any
contraints on the type parameter, all you can do is manipulate it
abstractly. It works well for all kind of data structures and
containers, but fails to express reusable components.

For instance in haXe you can do the following

interface Required {
...
}

class Api< T : Required > {
}

That means that in the implementation of Api you can call any method
defined in "Required". The type parameter is restricted to classes
"implementing" Required.

It is a bit difficult to deal with these contrainted type parameters
when having type inference at the same time, but this should not be a
problem of ES4.

> 3. How will static code be exectuted in such classes?
> 
>    class Example.<A> {
> 
>         {
>             // when does code that is placed here run?
>             // and, consequently, can it determine A?
>         }
> 
>         static var a:A;
>    }

Type parameters are usualy forbidden in static variables, since it does
not makes sense. Allowing them creates a type hole since you can do

Example.<Int>.a = 3
var s : String = Example.<String>.a;

> 4. How would reflection implementations be affected by type
> parameters? I'm thinking of AS3 here.

This is an interesting question. haXe has reflection (also when
targeting Flash9) but does not enforce type parameters at runtime. Two
reasons for that :

a) the Flash9 VM does not support them

b) haXe philosophy is about providing maximum compile-time typing and
maximum runtime flexibility. So enforcing too much typing at runtime
would prevent the second goal. This is for instance the reason Java is
so much static.

But this might not be the case for ES4...

Nicolas



More information about the Es4-discuss mailing list