Version française
Home     About     Download     Resources     Contact us    
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 http://caml.inria.fr/caml-list/1005.html.

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

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

second.mli
----------

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

module Run (Args : S) : 
  sig 
    val entry_point : unit -> unit
  end

second.ml
---------
module type S =
  sig
    type type0 = ...
    val const0 : type0
    ...
  end

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

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:

main.ml
-------

module Args =
  struct
    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
    ...

  end

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

--------------------------------------------------------------------
Christian Rinderknecht          Christian.Rinderknecht@polyspace.com
PolySpace Technologies          Tel: 04.76.61.54.17
c/o INRIA                       Fax: 04.76.61.54.09
655, Av. de l'Europe            http://www.polyspace.com/
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).