Browse thread
[Caml-list] subtyping and inheritance (bug?)
-
james woodyatt
-
Jacques Garrigue
-
Eray Ozkural
- Brian Rogoff
-
Eray Ozkural
-
Jacques Garrigue
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Brian Rogoff <bpr@a...> |
| Subject: | Re: [Caml-list] subtyping and inheritance (bug?) |
Eray Ozkural writes:
> On Tuesday 25 June 2002 05:54, Jacques Garrigue wrote:
> > 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.
>
> Which programming style would express the intended semantics, then? I guess we
> would need a slightly different formulation.
Yes, you need a different formulation. If you want to have a tree class but
want to create a family of color_trees which can be coerced to each other
and put on the same list, something like this
class ['a] tree =
object
val v = (None : string option)
val ch = ([] : 'a list)
method value_get = v
method value_set x = {< v = x >}
method children_get = ch
method children_set x = {< ch = x >}
end;;
class color_tree =
object
inherit [color_tree] tree
method color = "green"
end;;
class pine_tree =
object
val mutable age = 0.0
inherit color_tree
method color = "evergreen"
method age = age
end;;
class fake_tree =
object
inherit color_tree
method color = "olivegreen"
method material = "fantastic plastic"
end;;
now these can all be coreced to color_tree, and they have a colortree list
type on method children_get.
let colortree = new color_tree;;
let pinetree = new pine_tree;;
let faketree = new fake_tree;;
let trees = [colortree;
(pinetree :> color_tree);
(faketree :> color_tree)];;
faketree#children_set trees;;
If you create a new class,
class palm_tree =
object
inherit [palm_tree] tree
method color = "palmgreen"
method fronds = true
end;;
then you won't be able to do the coercion.
let palmtree = new palm_tree;;
(palmtree :> color_tree);; (* ERROR! *)
-- Brian
-------------------
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