From: William Harold Newman <william.newman@airmail.net>
To: caml-list@inria.fr
Subject: [Caml-list] difficulties narrowing OO types
Date: Wed, 14 Nov 2001 11:17:08 -0600 [thread overview]
Message-ID: <20011114111708.A14071@rootless> (raw)
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
next reply other threads:[~2001-11-14 17:42 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-11-14 17:17 William Harold Newman [this message]
2001-11-15 1:05 ` Jacques Garrigue
2001-11-15 6:43 ` Jacques Garrigue
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20011114111708.A14071@rootless \
--to=william.newman@airmail.net \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox