From: Brian Hurt <bhurt@spnz.org>
To: Michael Vanier <mvanier@cs.caltech.edu>
Cc: garrigue@kurims.kyoto-u.ac.jp, <caml-list@inria.fr>
Subject: Re: [Caml-list] Re: OCAML Downcasting?
Date: Tue, 21 Sep 2004 16:38:25 -0500 (CDT) [thread overview]
Message-ID: <Pine.LNX.4.44.0409211619370.5809-100000@localhost.localdomain> (raw)
In-Reply-To: <20040921192719.92B859BD95@orchestra.cs.caltech.edu>
On Tue, 21 Sep 2004, Michael Vanier wrote:
> But java has interfaces (subtypes), and classes can implement interfaces
> (making them subtypes of the interface) without inheritance. Furthermore,
> the class identity can be retrieved from an instance of an interface by
> using the instanceof operator.
Java also has run time type identification, something Ocaml doesn't have.
And generally doesn't need. Downcasting, however, requires RTTI- I hand
you an object of class foo, you think it's really an object of class bar
(which inherits from foo), and want to turn the foo into a bar- but how do
you know it's *really* a bar? It might just be a foo, it might be a baz
(a different class inheriting from foo).
The general solution is to rethink the problem to avoid the downcast.
Some general platitudes: first, avoid upcasts. Try using column types
instead. Second, move the operations that need the downcast into the
object itself- delegate (the object knows what sort of object it really
is, even if you don't). Third, ditch objects. Java, C++, etc. strongly
encourage you to think of everything as an object, try designing the code
sans objects and see how it works. Fourth, hand implement RTTI- either by
adding member functions to the base class to do the conversion (throwing
an exception if the conversion is bad), or using a variant type.
> > - classes may have type parameters. Assume a situation like
> > class ['a] a = object ... end
> > Then we cannot downcast any runtime object to "a", because we don't
> > know what is its type parameter. Again, downcast would not mix well
> > with other features of the language.
>
> Java now has generics (type parameters), and I assume that it has to get
> around this situation somehow.
If I understand it correctly, they basically lifted C++'s templates more
or less verbatim. I don't have experience with Java Generics, but C++
templates create a whole new copy of the class for every type they handle.
So that List<short>, List<int>, List<float>, List<foo_t>, etc. all use
different instantiations. How C++ gets around it is that if you have a
template<class t> class foo_t, bar_t doesn't so much inherit from foo_t,
as bar_t<int> inherits from foo_t<int>, etc.
>
> >
> > So, while this is somehow unfortunate, there is no downcast in ocaml.
> >
>
> I'm sure there are good reasons for this, but I don't find the arguments
> you've presented above persuasive. Not that I'm trying to hold java up as
> a shining example of the Right Thing; I'd much rather program in ocaml than
> java any day of the week. But the lack of downcasting has frustrated me in
> the past (it's my #1 gripe about ocaml, with the lack of support for
> native-code shared libraries at #2).
Downcasting is a sign you're doing something wrong. If you knew the
object was a bar_t, why didn't it have the type bar_t from the get-go? If
you don't know it's a bar_t, how do you know it's safe to cast it to a
bar_t? Without RTTI, that is.
By the way, RTTI costs memory (at least one extra word per object, plus
the overhead of the Class classes for all types). Plus- does RTTI apply
to non-objects? Ocaml has a lot of types that are not classes.
--
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
- Gene Spafford
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
next prev parent reply other threads:[~2004-09-21 21:29 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <ci7tcf$qqf$1@wolfberry.srv.cs.cmu.edu>
[not found] ` <ci9ggm$i6p$1@wolfberry.srv.cs.cmu.edu>
2004-09-21 8:03 ` Jacques GARRIGUE
2004-09-21 8:43 ` Damien Pous
2004-09-21 9:15 ` Jacques GARRIGUE
2004-09-21 9:29 ` skaller
2004-09-21 9:49 ` Jacques GARRIGUE
2004-09-21 9:34 ` Stefano Zacchiroli
2004-09-21 9:56 ` Jacques GARRIGUE
2004-09-21 19:27 ` Michael Vanier
2004-09-21 21:38 ` Brian Hurt [this message]
2004-09-21 22:06 ` Michael Vanier
2004-09-21 22:32 ` Brian Hurt
2004-09-22 1:04 ` skaller
2004-09-21 22:20 ` Marcin 'Qrczak' Kowalczyk
2004-09-22 2:26 ` skaller
2004-09-22 6:31 ` Marcin 'Qrczak' Kowalczyk
2004-09-22 9:03 ` sejourne_kevin
2004-09-22 10:29 ` Richard Jones
2004-09-22 18:39 ` Brian Hurt
2004-09-22 10:50 ` skaller
2004-09-22 12:03 ` Alain Frisch
2004-09-22 12:50 ` Cláudio Valente
2004-09-22 13:15 ` Marcin 'Qrczak' Kowalczyk
2004-09-22 15:50 ` skaller
2004-09-22 18:42 ` Brian Hurt
2004-09-22 18:44 ` Marcin 'Qrczak' Kowalczyk
2004-09-22 19:18 ` Brian Hurt
2004-09-22 0:50 ` skaller
2004-09-22 1:30 ` Jacques GARRIGUE
2004-09-22 2:59 ` skaller
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=Pine.LNX.4.44.0409211619370.5809-100000@localhost.localdomain \
--to=bhurt@spnz.org \
--cc=caml-list@inria.fr \
--cc=garrigue@kurims.kyoto-u.ac.jp \
--cc=mvanier@cs.caltech.edu \
/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