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
How to cleanly encode "quasi-constants"?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Christian Rinderknecht <rinderkn@p...>
Subject: Re: How to cleanly encode "quasi-constants"?
Hi David,

I think it's worth reading the caml-list thread "one-time initialization",
starting at

Personnally, in the particular situation you mention, I would do the

In a Second(ary) module you define a functor taking as argument a module
with all the globals you need.


module type S =
    type type0 = ...
    val const0 : type0

module Run (Args : S) : 
    val entry_point : unit -> unit
module type S =
    type type0 = ...
    val const0 : type0

module Run = functor (M : S) ->
    open M
    ... (* From here you handle "real" constants" [const0] etc. *)
    let entry_point () = ...

By the way, note that in OCaml, contrary to SML, you cannot define a
functor at top-level (this would require extra syntax), i.e. whose name is
mapped to a file name.

Now in your Main module:

module Args =
    type type0 = ...
    let quasi_const0 : (type0 option) ref = ref (None)

    let _ = Arg.parse .... (* Initialization by side-effects of
                              [quasi_const0] etc. *)

    let const0 = 
      match !quasi_const0 with
        None -> failwith "Missing arg" (* Or whatever *)
      | Some (v) -> v


module Go = Second.Run (Args)

let _ = Go.entry_point ()

Note that name [Go] is necessary (Second.Run(Args).enty_point() does
not work).

Hope this helps,


Christian Rinderknecht
PolySpace Technologies          Tel:
c/o INRIA                       Fax:
655, Av. de l'Europe  
F-38330 Montbonnot St Martin

On 20 Jun 2000, David Mentré wrote:

> In my program, I have variables that can only be modified once
> (potentially at initialization time after parsing command line and done
> some computation) and then are never modified for a very long time (rest
> of program execution).