Version française
Home     About     Download     Resources     Contact us    
Browse thread
class subtyping
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Martin Sandin <om_sandin@y...>
Subject: Re: class subtyping
nakata keiko wrote:
> Why an instance of class a can not coerced into type b?

I'll assume you mean the other way around :-)

> class a = object (self : 'a)
>   method f (x : 'a) = x#g
>   method g = 0
> end
> 
> class b = object 
>   inherit a
>   method h = 1
> end
> 
> let x = ((new b) :> a)

Afaik the self type refers to actual type of the object, eg in object
of class b it refers to type b. Thus, the method f of class b
requires an object of type b as a parameter, and type b implies
having a method h. Objects of class a don't have this method,
and so b#f can't accept all the arguments a#f can. Google for co/contra 
variance for discussions of these issues.

The below class avoids the problem:

class a = object
   method f (x : a) = x#g
   method g = 0
end

/Martin