Self type

Eylon Stroh estroh at adobe.com
Fri Aug 17 12:13:22 PDT 2007


Peter,

This was my confusion, as well.  I was about to send a reply to Cormac's
email but, in working through my reply, I realized what was going on.

The types are not quite equivalent to substituting T1 and T2, because
Self is the concrete object type which needs to be a subtype of the
defined type but not exactly the same, so the equivalent expressions
are:

type T1 = { f:function(this:t1, z:t1):t1, w:int } for some t1 <: T1

type T2 = { f:function(this:t2, z:t2):t2 } for some t2 <: T2

This means that 

var x:T1 = { ... }:T1; var y:T2 = x;

is not an error, because T1 is a legitimate candidate for t2 <: T2.

-- Eylon --


-----Original Message-----
From: es4-discuss-bounces at mozilla.org
[mailto:es4-discuss-bounces at mozilla.org] On Behalf Of Peter Hall
Sent: Friday, August 17, 2007 7:35 AM
To: Cormac Flanagan
Cc: Brendan Eich; es4-discuss
Subject: Re: Self type

I think my confusion here, is perhaps that I'm seeing this a slightly
different way. What you are saying seems to suggest that Self type
annotations can behave differently at runtime. To me, this example from
the wiki should be an error:

type T1 = { f:function(this:Self, z:Self):Self, w:int} var x:T1 = { ...
}:T1 type T2 = { f:function(this:Self, z:Self):Self } var y:T2 = x; //
ERROR

Because the types are equivalent to:

type T1 = { f:function(this:T1, z:T1):T1, w:int} type T2 = {
f:function(this:T2, z:T2):T2}

So T1's f and T2's f are incompatible. In a real situation, an author
would have to be a bit more careful about how the types are defined, and
probably use wildcard types or Object (or perhaps fully expanded type
definitions) where necessary. But I think that is better than having
wildcards inserted automatically behind the scenes.


Peter

On 8/14/07, Cormac Flanagan <cormac at soe.ucsc.edu> wrote:
>
>
> Peter Hall wrote:
> > On 8/14/07, Eylon Stroh <estroh at adobe.com> wrote:
> >> From the proposal:
> >> "The return type T2 is ok, even if the function returns a T1
instead...
> >> covariant occurrences of Self (eg in the result type) are replaced 
> >> by T2"
> >>
> >
> > I was also wondering about this.
> > To me, if someone writes "Self" for an argument or return type, they

> > probably mean T1. If they actually want it to be "*", why not let 
> > them write "*"? Or is there something else going on here?
>
> Yes, it would be intuitive to replace "Self" by T1, but it would be 
> unsound, since Self really means:
>
>    the actual allocated type of the current object, which is some
unknown
>    subtype of the known static type T1 of that object
>
> so we can only safely replace Self by T1 in "covariant" contexts 
> (where we get stuff out, and don't care if we actually get a subtype),

> but not in "contravariant" contexts, such as argument positions.
>
> - Cormac
> _______________________________________________
> Es4-discuss mailing list
> Es4-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es4-discuss
>
_______________________________________________
Es4-discuss mailing list
Es4-discuss at mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss



More information about the Es4-discuss mailing list