Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] subtyping and inheritance (bug?)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] subtyping and inheritance (bug?)
From: james woodyatt <jhw@wetware.com>

> Consider the following variation on a theme currently found in another 
> thread on this list:
> 
> > class tree =
> >   object (_:'self)
> >     val v = (None : string option)
> >     val ch = ([] : 'self list)   (* NOTE: 'a list is co-variant in 'a *)
> >     method value_get = v
> >     method value_set x = {< v = x >}
> >     method children_get = ch
> >     method children_set x = {< ch = x >}
> >   end;;
> >
> > class colortree =
> >   object
> >     inherit tree
> >     method color = "evergreen"
> >   end;;
> >
> > ((new colortree) :> tree);;  (* ERROR! *)
> 
> Here, the compiler (ocaml-3.04) complains that objects of class subtree 
> cannot be coerced to class tree.

Sure. The children_set method contains a contravariant occurence of 'self.
It cannot be compatible with its subclasses.  You don't need to have
mutable data to have variance incompatibilities.
No bug, business as usual.

Jacques Garrigue
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners