caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: brogoff@speakeasy.net
To: Michael Vanier <mvanier@cs.caltech.edu>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] a design problem requiring downcasting? (long)
Date: Thu, 26 Sep 2002 10:47:47 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.44.0209261022570.22090-100000@grace.speakeasy.net> (raw)
In-Reply-To: <200209260901.g8Q910t03459@orchestra.cs.caltech.edu>

On Thu, 26 Sep 2002, 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.

I'm not so sure about your specific problem, but when I've wanted my class 
hierarchy to behave more like a sum type I've simply grafted a sum type over the 
set of class types (or virtual classes). Here's a sketch for a composite 

type ('a, 'b) node = Leaf_node of 'a | Hier_node of 'b
and ('a, 'b) instance =
    CellRef of Atom.t * ('a, 'b) node * Geometry.placement
  | CellArrayRef of
      Atom.t * ('a, 'b) node * Geometry.placement * Gdsii.colrow * ipoint * ipoint

class virtual leaf_intf  =
  object
    method virtual full_view : (leaf_intf, hier_intf) node
    (* ... etc... *)
  end 
and virtual hier_intf =
  object
    inherit leaf_intf
    method virtual leaf_view : (leaf_intf, hier_intf) node
    method virtual insts : (leaf_intf, hier_intf) instance list
  end

let as_leaf : (leaf_intf,hier_intf) node -> leaf_intf = function 
    Leaf_node(l) -> (l :> leaf_intf)
  | Hier_node(h) -> (h :> leaf_intf)

let  as_hier : (leaf_intf,hier_intf) node -> hier_intf = function 
  (* ...etc... *)

Obviously, this only works well if you have a fairly small number of 
"abstract base classes", since each one is a type parameter. You don't even need 
to use parametric polymorphism, and you could just declare a type specific to 
this class hierarchy; take a look at 

http://cristal.inria.fr/~remy/cours/appsem/ocaml049.html#toc22

where Didier Remy gives an example using variants and projection functions. 

Also, the weak library mentioned elsewhere looks promising. 

As I get older and lazier, I wonder more if a lot of the OO approaches are 
really easier to grasp. A lot of people are going right for the O before going 
through Caml, and that seems wrong. 

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


  parent reply	other threads:[~2002-09-26 17:47 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 [this message]
2002-09-26 19:14 ` Fred Smith
2002-09-27 17:01   ` Tim Freeman
2002-09-26 22:46 ` Alessandro Baretta
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=Pine.LNX.4.44.0209261022570.22090-100000@grace.speakeasy.net \
    --to=brogoff@speakeasy.net \
    --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).