Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Conditional Modules
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Shivkumar Chandrasekaran <shiv@e...>
Subject: Re: [Caml-list] Conditional Modules
I find the following paradigm useful sometimes:

module Params = struct
    let param1 = ... (* from command line args *)
    let param2 = ...

module M1 = F1 (Params, ...) (* specialize modules to command line args *)
module M2 = F2 (Params, ...)
module M3 ...

(* call correct main routine *)
if predicate1 args then
    M1.main ()
else if predicate2 args then
    M2.main ()
else if ...

In other words, fold the whole main function into the functors. Then 
only the main routine in each module must return same type and the 
individual modules themselves are free of each other.

Of course you can do this for functions other than main too.


John Prevost wrote:

>If you're choosing between options at runtime, you might consider
>thinking about a plugin architecture instead.  Modules are pretty much
>all set at link time.
>Here's a simple example of the strategy:
>module M = struct
>  exception No_plugin
>  type entry = { reg_f : int -> int }
>  let current_entry : entry option = ref None
>  let register e = current_entry := Some e
>  let get_entry () = match !current_entry with
>    | None -> raise No_plugin
>    | Some e -> e
>  let f x = (get_entry ()).f x
>module M1 = struct
>  let f x = x + 1
>  let m1_entry = { M.reg_f = f }
>  let _ = M.register m1_entry  
>This model puts more constraints on types than using modules would,
>but does have a variety of different ways to be useful.  For example,
>M1 could instead of registering automatically wait until told, or M
>could keep a list of registered plugins and take the first one that
>"works" for an input, etc.
>And finally, you may also be able to do things differently using
>functors--one of which you seem to be using in your example.  Here's
>an example of my idea here:
>module M = if arg then M1 else M2
>type t = M.t
>let f = M.f
>let g = M.g
>can instead be written:
>module X (M : M_T) : R_T with type t = (* something *) = struct
>  (* definitions involving M *)
>module X1 = X(M1)
>module X2 = X(M2)
>type t = X1.t
>let f = if arg then X1.f else X2.f
>let g = if arg then X1.g else X2.g
>This approach is a little verbose, but should work reasonably well. 
>Again, your constraint here is that X1 and X2 will have to have
>identical types, not just similar types.  (And that's the real reason
>that you can't do conditionals to choose one of several modules: you
>can't do conditionals at runtime on types!)
>To unsubscribe, mail Archives:
>Bug reports: FAQ:
>Beginner's list:

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: