caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: nadji@noos.fr
To: Michael Vanier <mvanier@cs.caltech.edu>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] a design problem requiring downcasting? (long)
Date: Thu, 26 Sep 2002 17:19:21 +0200	[thread overview]
Message-ID: <3D932579.1839FFF2@noos.fr> (raw)
In-Reply-To: <200209260901.g8Q910t03459@orchestra.cs.caltech.edu>

Hi,

If I understood your problem, Jacques Garrigue has shown a way
to allow "safe" downcast, that is to say that if you can't downcast you
cleanly get an exeption. You are required to register your class for
all the downcasts you wants, and it will work only for them (i.e. if
you don't register you can't downcast). This is the "memo" class
in the code below.
Following this idea, your simulation manager has to define all the interfaces it
will be using, and the simulations have to register themselves before
beeing used.
Code follows :

(* In simulation infrastructure *)
module Sim = struct
  class ['ct] memo = object
    val h = Hashtbl.create 0
    method add : 'ct -> unit = fun x -> Hashtbl.add h (Obj.repr x) x
    method get : 'a . 'a -> 'ct = fun x -> Hashtbl.find h (Obj.repr x)
  end

  class type base = object
    method simname : string
  end

  class type ct1 = object
    inherit base
    method a : string
  end

  class type ct2 = object
    inherit base
    method b : int
    method bin : 'a. (#ct1 as 'a) -> string
  end

  class type ct3 = object
    inherit base
    method a : string
    method b : int
  end

  let h1 : ct1 memo = new memo
  let h2 : ct2 memo = new memo
  let h3 : ct3 memo = new memo

  let nbsims = ref 0
  let thesims : (int, base) Hashtbl.t = Hashtbl.create 0

  let test a b =
    let obj1 = h1#get a in
    let obj2 = h2#get b in
    obj2#bin obj1
end

(* in sim 1 *)
class class1 = object (self)
  method simname = "sim1"
  method a = "foo"
  method b = 4
  initializer
    Sim.h1#add (self:>Sim.ct1);
    Sim.h3#add (self:>Sim.ct3);
end

(* in sim 2 *)
class class2 = object (self)
  method simname = "sim2"
  method b = 5
  method bin : 'a. (#Sim.ct1 as 'a) -> string =
    fun x -> x#a

  initializer
    Sim.h2#add (self:>Sim.ct2);
end

This is only a suggestion, there are certainly other ways
to achieve what you want.
HTH,
Nadji

-------------------
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 15:19 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 [this message]
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
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=3D932579.1839FFF2@noos.fr \
    --to=nadji@noos.fr \
    --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).