* [Caml-list] subtyping and inheritance (bug?)
@ 2002-06-25 2:46 james woodyatt
2002-06-25 2:54 ` Jacques Garrigue
0 siblings, 1 reply; 4+ messages in thread
From: james woodyatt @ 2002-06-25 2:46 UTC (permalink / raw)
To: The Trade
everyone--
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
fixed?
(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 <jhw@wetware.com>
-------------------
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] subtyping and inheritance (bug?)
2002-06-25 2:46 [Caml-list] subtyping and inheritance (bug?) james woodyatt
@ 2002-06-25 2:54 ` Jacques Garrigue
2002-06-25 12:22 ` Eray Ozkural
0 siblings, 1 reply; 4+ messages in thread
From: Jacques Garrigue @ 2002-06-25 2:54 UTC (permalink / raw)
To: jhw; +Cc: caml-list
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] subtyping and inheritance (bug?)
2002-06-25 2:54 ` Jacques Garrigue
@ 2002-06-25 12:22 ` Eray Ozkural
2002-06-25 21:33 ` Brian Rogoff
0 siblings, 1 reply; 4+ messages in thread
From: Eray Ozkural @ 2002-06-25 12:22 UTC (permalink / raw)
To: Jacques Garrigue, jhw; +Cc: caml-list
On Tuesday 25 June 2002 05:54, Jacques Garrigue wrote:
> From: james woodyatt <jhw@wetware.com>
>
> 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.
Thanks,
--
Eray Ozkural <erayo@bilkent.edu.tr>
Comp. Sci. Dept., Bilkent University, Ankara
www: http://www.cs.bilkent.edu.tr/~erayo
GPG public key fingerprint: 360C 852F 88B0 A745 F31B EA0F 7C07 AE16 874D 539C
-------------------
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] subtyping and inheritance (bug?)
2002-06-25 12:22 ` Eray Ozkural
@ 2002-06-25 21:33 ` Brian Rogoff
0 siblings, 0 replies; 4+ messages in thread
From: Brian Rogoff @ 2002-06-25 21:33 UTC (permalink / raw)
To: erayo; +Cc: garrigue, jhw, caml-list
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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2002-06-25 21:35 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-06-25 2:46 [Caml-list] subtyping and inheritance (bug?) james woodyatt
2002-06-25 2:54 ` Jacques Garrigue
2002-06-25 12:22 ` Eray Ozkural
2002-06-25 21:33 ` Brian Rogoff
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox