caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] difficulties narrowing OO types
@ 2001-11-14 17:17 William Harold Newman
  2001-11-15  1:05 ` Jacques Garrigue
  0 siblings, 1 reply; 3+ messages in thread
From: William Harold Newman @ 2001-11-14 17:17 UTC (permalink / raw)
  To: caml-list

OCaml seems generally reluctant to narrow OO types.

E.g., dynamically casting from a base class to a subclass is
unsupported (though I've seen the hack to work around this in Jason
Hickey's online introduction). And as far as I can tell, it's
impossible for a method on a subclass to guarantee that it returns a
narrower type than the method on the base class.

I'm still trying to understand the language, so it was only as I was
trying to express what I thought was an analogous limitation for
variant types I realized that there seems to be no such limitation
after all. I can define types like
  type tagged_reg = { tagged_n : int } ;;
  type untagged_reg = { untagged_n : int } ;;
  type reg = Tagged_Reg of tagged_reg | Untagged_Reg of untagged_reg ;;
and then define things like
  let rec tagged_regs regs = match regs with
  | [] -> []
  | Tagged_Reg(tr) :: rest -> tr :: tagged_regs rest
  | Untagged_Reg(_) :: rest -> tagged_regs rest ;;
But I still don't see how to do things like this with classes.

My immediate motivation for thinking about narrowing types is that I
have a class "sq" which lives on a grid where I know from global
constraints that all the other "sq" objects on the grid are of the
same class, and furthermore that this will be true of any subclasses.
I.e. if it's a pure "sq", all elements of its grid will be pure "sq"
also, and it it's a subclassed objects "subsq" all elements will be
"subsq", and so on for "subsubsq" and "subsubsubsq" and so forth. I'd
like to be able to define a "neighbors" method which returns a list of
"sq" when the object is exactly of class "sq", and returns a list of
"subsq" when the object is exactly of class "subsq", and so on. Or,
I'd be satisfied with a "map_on_neighbors" function which mapped onto
neighboring "sq" objects in the "sq" case, and neighboring "subsq"
objects in the "subsq" case, and so forth. An approximate analogy in
the register example above might be functions like "colliding_regs" or
"alternative_regs" which are meaningful both for tagged and untagged
registers, and return only values of the same type as their register
argument.

If there really are systematic obstacles to narrowing OO types in
OCaml, not just beginner confusion on my part, is there some deep
reason? Perhaps because it causes problems with type inference? At
first I thought it might be because it tends to be strongly correlated
with typecase operations which miss the point of OO, but that doesn't
explain why I can't narrow the return type of a subclass method.

-- 
William Harold Newman <william.newman@airmail.net>
PGP key fingerprint 85 CE 1C BA 79 8D 51 8C  B9 25 FB EE E0 C3 E5 7C
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2001-11-15  6:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-14 17:17 [Caml-list] difficulties narrowing OO types William Harold Newman
2001-11-15  1:05 ` Jacques Garrigue
2001-11-15  6:43   ` Jacques Garrigue

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).