Browse thread
[Caml-list] a design problem requiring downcasting? (long)
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | nadji@n... |
| Subject: | Re: [Caml-list] a design problem requiring downcasting? (long) |
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