Browse thread
Rephrasing of dynamic module selection problem
-
Nathan Cooprider
- Martin Jambon
- brogoff
- Virgile Prevosto
[
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: | 2006-02-21 (03:50) |
From: | Martin Jambon <martin_jambon@e...> |
Subject: | Re: "ocaml_beginners"::[] Rephrasing of dynamic module selection problem |
On Mon, 20 Feb 2006, Nathan Cooprider wrote: > So I am still trying to get modules to be dynamically (run-time) > selectable instead of only statically (compile-time). The closest I have > come to so far is bellow. I want to be able to choose between a set of > modules (hello1 and hello2 in this example) fairly transparently. > > [coop@ender example]$ cat hello1.ml > type t = int > let of_int i = > i > let print i = > print_int i; > print_string " says Hello1\n" > > [coop@ender example]$ cat hello2.ml > type t = float > let of_int i = > float_of_int i > let print i = > print_float i; > print_string " says Hello2\n" > > [coop@ender example]$ cat main.ml > module Hello1 = struct > #include "hello1.ml" > end ;; > module Hello2 = struct > #include "hello2.ml" > end ;; > (* This works . . . *) > module H = Hello1 > (* But I would like this to be something like this instead: > let parameter = 1 > module H = > match parameter with > 1 -> Hello1 > | _ -> Hello2 > *) That's not possible because Hello1 and Hello2 don't have the same "type" (same module signature though): what I mean is that if you represent them as objects, the objects would have different types since Hello1.t and Hello2.t are incompatible. Try the following code (which fails when I try to mix the objects): module type HELLO = sig type t val of_int : int -> t val print : t -> unit val obj : < of_int : int -> t; print : t -> unit > end module Hello1 : HELLO = struct type t = int let of_int i = i let print i = print_int i; print_string " says Hello1\n" let obj = object method of_int = of_int method print = print end end module Hello2 : HELLO = struct type t = float let of_int i = float_of_int i let print i = print_float i; print_string " says Hello2\n" let obj = object method of_int = of_int method print = print end end let param = 2 (* defined at runtime *) let obj = match param with 1 -> Hello1.obj | 2 -> Hello2.obj | _ -> assert false;; Characters 63-73: | 2 -> Hello2.obj ^^^^^^^^^^ This expression has type < of_int : int -> Hello2.t; print : Hello2.t -> unit > but is here used with type < of_int : int -> Hello1.t; print : Hello1.t -> unit > Types for method of_int are incompatible So if you want to do that, you must give the same type to your objects. That would work if your modules Hello* all use a common type t instead of their own. In this case you just have to include the same code at the end of each Hello* module so that obj is defined. You can also add a line which places obj in a global table, e.g. let obj = ... let _ = Hashtbl.add Hello_modules.tbl 1 obj And your main program would be: let param = ... let obj = Hashtbl.find Hello_modules.tbl param in ... Martin -- Martin Jambon, PhD http://martin.jambon.free.fr Visit http://wikiomics.org, the Bioinformatics Howto Wiki