Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Re: class subtyping
[ Home ] [ Index: by date | by threads ]
[ Search: ]

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

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
and so b#f can't accept all the arguments a#f can.
Google for co/contra variance for discussions of these

The below class avoids the problem:

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


Do you Yahoo!? 
Yahoo! Mail - Find what you need with new enhanced search.