Nature of Parametrized types

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

Nature of Parametrized types

Peter Hall-4
I have a few questions/comments about parametrized types. Some I know
has been discussed, but I still unclear on the outcome of those
conversations.

1. If there is a type T.<J,K>, can T be referenced itself, or must it
be referenced for some specific values of J and K?

That is, can I say:
T1 = T;
or must it always be:
T1 = T.<A, B>;
My understanding is that, conceptually, T is not a type by itself but
that a type is generated each time that a different combination of
parameters is supplied. Is this understanding correct? If so, it
pretty much enforces that static properties are not shared between
each parametrization.


2. Is it legal to extend a general parametrized class, or may you only
extend a specific parametrization?
ie, is this allowed:
class T.<A> { var x:A; }
class S.<A> extends T { }
?
This seems to have all kinds of problems. For one, there is no
equivalent of "super()" to pass the parameters to the super class.
However, certain uses of parametrized classes might be unfeasible if
you can't extend the class without choosing a parametrization.


3. It seems that there are use-cases for statics being
per-parametrization and also for them being global for all
parametrizations. If I wanted to keep a count of all the instances of
a parametrized class had been created, I'd need a static var to be
global across all parametrizations. But there are probably more use
cases for it to be per-parametrization. So it would actually be really
nice if you could choose between these different scopes. Without
introducing new keywords, using a double "static static" could
indicate that a property is static across all parametrizations, while
a single  "static" would indicate that a property was static on a
per-parametrization basis.
e.g:

class A.<K> {
  public static static var a:int = 0;
  public static var b:int = 0;
}

A.<int>.a ++;
A.<int>.b ++;

A.<string>.a; // 1
A.<string>.b; // 0

I suppose an alternative would be a magic always-open namespace.


Peter
_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Nature of Parametrized types

Graydon Hoare-3
Peter Hall wrote:
> I have a few questions/comments about parametrized types. Some I know
> has been discussed, but I still unclear on the outcome of those
> conversations.

Good questions!

> 1. If there is a type T.<J,K>, can T be referenced itself, or must it
> be referenced for some specific values of J and K?
>
> That is, can I say:
> T1 = T;
> or must it always be:
> T1 = T.<A, B>;
> My understanding is that, conceptually, T is not a type by itself but
> that a type is generated each time that a different combination of
> parameters is supplied. Is this understanding correct? If so, it
> pretty much enforces that static properties are not shared between
> each parametrization.

This question is also raised in tickets #247 and #249, as well as some
recent committee discussion. The conclusion I think we've reached is to
immitate C++ and C#, not Java. Or as Lars summarized, given this class:

 >   class Cls.<T> {
 >       static var v;
 >   }

we are going to spec the following:

 >
 >   - Cls.v is nonsensical
 >   - You have to write Cls.<int>.v
 >   - Cls.<int>.v is different from Cls.<bool>.v
 >   - The result of
 >
 >       var p = Cls.<T>
 >       var q = Cls.<T>
 >       p === q
 >
 >     is always true.
 >

In other words, there is no sharing between instantiations on non-equal
type arguments, but all instantiations on equal type arguments should
produce the same (memoized) instantiated type / class, with sharable
static slots on the classes.

At present, I believe the RI is in violation of these rules. But it is
the intended position.

Note that this is not to say that the type "Cls" is nonsensical on its
own. Some operations are possible with it: it can be compared to other
types, it can be captured as a value and passed around, and it can be
instantiated with argument types. You just can't "reach into" it before
it's been parameterized, any more than you can "reach into" an
un-applied function.

> 2. Is it legal to extend a general parametrized class, or may you only
> extend a specific parametrization?

Only a specific parameterization.

> 3. It seems that there are use-cases for statics being
> per-parametrization and also for them being global for all
> parametrizations.

There are certainly some plausible use cases, but they are (I think)
beyond the scope of what we're doing this time around. As are quite a
number of extensions to parametric types we've seen in the wild. If they
become pressing in the future, future editions may address them.

-Graydon

_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Nature of Parametrized types

Peter Hall-4
>  Note that this is not to say that the type "Cls" is nonsensical on its
>  own. Some operations are possible with it: it can be compared to other
>  types, it can be captured as a value and passed around, and it can be
>  instantiated with argument types. You just can't "reach into" it before
>  it's been parameterized, any more than you can "reach into" an
>  un-applied function.
>

Ok. Does "Cls.<int> is Cls" evaluate to true? ie, could the
specification be written so that:

     class Cls.<T1,T2> extends S {  /* code using T1 and T2 */ }

is compiled as if you had written:

     class Cls extends S {   }

but somehow internally marked with the parameters that are required in
order to instantiate it.

And, on encountering references to Cls.<string,int>, generate an
unnamed class  as if created by the following ES4 code:

     class Cls_string_int extends Cls { /* replace T1,T2,.. here */}

So that:

    new Cls.<string,int> is Cls;

evaluates to true.

Then it could also allow code like this:

     class MyCls.<S1> extends Cls.<S1,int> {  /* code using S1 */  }

So, when MyCls.<string> is referenced, it would generate an unnamed
class as if written:

     class MyCls_string extends Cls_string_int {  /* .... */  }

And the "is" relationship holds throughout.

The only obvious niggle is that the Java behaviour for statics would
naturally emerge, so you would have to take them into special account
to get the behaviour you want.


>
>  > 2. Is it legal to extend a general parametrized class, or may you only
>  > extend a specific parametrization?
>
>  Only a specific parameterization.

See above. But I think this is quite an important thing not to leave
out. Once you have implemented something with parametrized types, it
could be a maintainability nightmare if, further down the line, you
find that you have to add functionality by subclassing.


>
>  > 3. It seems that there are use-cases for statics being
>  > per-parametrization and also for them being global for all
>  > parametrizations.
>
>  There are certainly some plausible use cases, but they are (I think)
>  beyond the scope of what we're doing this time around. As are quite a
>  number of extensions to parametric types we've seen in the wild. If they
>  become pressing in the future, future editions may address them.
>

If statics are always per-parametrization, then you can very easily
achieve the same effect as the other option by creating package-level
properties or statics on a separate class. It's not as nice to look
at, but quite acceptable. The other way around would be trickier.


Peter
_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Nature of Parametrized types

Waldemar Horwat
Doing this would require multiple inheritance if you tried to apply it to a two-level class hierarchy.

class A.<X>;
class B.<X> extends A.<X>;

Now consider where the unqualified type B would belong in the hierarchy.

    Waldemar


Peter Hall wrote:

>>  Note that this is not to say that the type "Cls" is nonsensical on its
>>  own. Some operations are possible with it: it can be compared to other
>>  types, it can be captured as a value and passed around, and it can be
>>  instantiated with argument types. You just can't "reach into" it before
>>  it's been parameterized, any more than you can "reach into" an
>>  un-applied function.
>>
>
> Ok. Does "Cls.<int> is Cls" evaluate to true? ie, could the
> specification be written so that:
>
>      class Cls.<T1,T2> extends S {  /* code using T1 and T2 */ }
>
> is compiled as if you had written:
>
>      class Cls extends S {   }
>
> but somehow internally marked with the parameters that are required in
> order to instantiate it.
>
> And, on encountering references to Cls.<string,int>, generate an
> unnamed class  as if created by the following ES4 code:
>
>      class Cls_string_int extends Cls { /* replace T1,T2,.. here */}
>
> So that:
>
>     new Cls.<string,int> is Cls;
>
> evaluates to true.
>
> Then it could also allow code like this:
>
>      class MyCls.<S1> extends Cls.<S1,int> {  /* code using S1 */  }
>
> So, when MyCls.<string> is referenced, it would generate an unnamed
> class as if written:
>
>      class MyCls_string extends Cls_string_int {  /* .... */  }
>
> And the "is" relationship holds throughout.
>
> The only obvious niggle is that the Java behaviour for statics would
> naturally emerge, so you would have to take them into special account
> to get the behaviour you want.
_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Nature of Parametrized types

Peter Hall-4
On Fri, Mar 21, 2008 at 10:41 PM, Waldemar Horwat <[hidden email]> wrote:
> Doing this would require multiple inheritance if you tried to apply it to a two-level class hierarchy.
>
>  class A.<X>;
>  class B.<X> extends A.<X>;
>
>  Now consider where the unqualified type B would belong in the hierarchy.

Yes, you are right... you would need B.<int> to extend both A.<int>
and B. But that could be solved, in the same spirit as my original
suggestion, by treating unqualified B as an empty interface, so as to
maintain the "is" relationship.

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