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
[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: 2002-06-25 (02:44)
From: james woodyatt <jhw@w...>
Subject: [Caml-list] subtyping and inheritance (bug?)

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.

Apparently, the problem is that colortree is only a subtype of tree if 
colortree list is a subtype of tree list, *BUT* colortree list is only a 
subtype of tree list if colortree is a subtype of tree, i.e. if the 
compiler cannot independently validate these two conditions, then the 
subtype relation is not inferred.

My question: is this a bug?  If so, is it known already, and will it be 
(I care about this because I'm finding that functional object style 
programming is a handy way to deal with problems implementing extensible 
state machines.)

In any case, I think I'd prefer a warning when a class type cannot be 
used as a supertype of its descendants.  The added expense of using 
classes instead of modules and functors is only really worth it, in my 
view, if you're using inheritance *and* subtyping.  I'm not sure I see 
any real value in being able to use inheritance to produce descendant 
classes that are not subtypes of their ancestors.

A warning, if not an error, would be better than nothing.

j h woodyatt <>

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: