Self type

Peter Hall peter.hall at
Fri Aug 17 07:35:14 PDT 2007

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.


On 8/14/07, Cormac Flanagan <cormac at> wrote:
> Peter Hall wrote:
> > On 8/14/07, Eylon Stroh <estroh at> 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

More information about the Es4-discuss mailing list