caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Alessandro Baretta <alex@baretta.com>
To: Michael Vanier <mvanier@cs.caltech.edu>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] a design problem requiring downcasting? (long)
Date: Fri, 27 Sep 2002 00:46:59 +0200	[thread overview]
Message-ID: <3D938E63.6000909@baretta.com> (raw)
In-Reply-To: <200209260901.g8Q910t03459@orchestra.cs.caltech.edu>



Michael Vanier wrote:
> I've run into a design problem that I think requires downcasting, and I
> wanted to see if anyone on the list has any ideas about an alternative
> approach, since AFAIK downcasting is impossible in the current ocaml object
> system (at least without nasty Obj.magic hacks).  I'll try to simplify the
> problem as much as possible, but this is still pretty long.
> <long explanation follows>
> 

Scratch... scratch... Hmmm...

I'm not really sure why you would want to use downcasts in 
your problem. I have been taught, as a general rule, that a 
downcast is (usually) a bug in the design and often also a 
bug in the implementation.

Downcasting in O'Caml is only useful when you have classes 
with *different* class types but sharing some common 
ancestor. Then you can keep a reference to the an object of 
the ancestor class and downcast it selectively to recover 
the methods added by inheriting classes. But, then again, if 
your design requires to keep a reference to a generic 
object, you are explicitly throwing out type information: in 
O'Caml upcasts are explicit, so you know *exactly* where you 
are throwing away type information. If you throw out type 
information and subsequently need a downcast to recover it, 
then you are probably working with a wrong abstraction 
somewhere in your analysis.

If I were you, I'd consider using a virtual base class as a 
common ancestor. Such a class would have to define all the 
public methods that might be called on any object. If a 
given method is truly meaningless within a specific class, 
you can always define it as raising a Meaningless_method 
exception, or something similar. But then you'd better make 
sure that your algorithm is correct, or it will die 
miserably with uncaught exceptions.

This is very much like downcasting, but it aids you in 
figuring out exactly what methods are creating the trouble. 
In my last project, I noticed that most typing problems with 
  classes were due to refinements of a base class being 
unified with their ancestor where this was not meaningful. 
It turned out that I could always solve this typing problem 
by isolating newly created objects of the inheriting 
classes, calling what additional methods the iheriting class 
defined in the appropriate order, and upcasting to the base 
class. The new object, thus initialized, could then be 
correctly type-restricted, without ever needing to recover 
the type information thrown away.

This strategy might not necessarily be appicable to your 
case, but I consider it, at least. It might save you a lot 
of hell. Remember: "If God wanted us to downcast, he would 
have given us RTTI."

Alex

-------------------
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


  parent reply	other threads:[~2002-09-26 22:37 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-09-26  9:01 Michael Vanier
2002-09-26 14:32 ` Remi VANICAT
2002-09-26 15:19 ` nadji
2002-09-26 15:27   ` Remi VANICAT
2002-09-26 15:55     ` nadji
2002-09-26 15:53 ` Jeffrey Palmer
2002-09-26 16:35 ` Oleg
2002-09-26 17:47 ` brogoff
2002-09-26 19:14 ` Fred Smith
2002-09-27 17:01   ` Tim Freeman
2002-09-26 22:46 ` Alessandro Baretta [this message]
2002-09-27  7:20 ` Francois Pottier
2002-09-27 10:16   ` Michael Vanier
2002-09-29 22:59     ` Alessandro Baretta
2002-09-30  7:09       ` Michael Vanier
2002-09-30  9:54         ` Alessandro Baretta

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=3D938E63.6000909@baretta.com \
    --to=alex@baretta.com \
    --cc=caml-list@inria.fr \
    --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;
as well as URLs for NNTP newsgroup(s).