Version française
Home     About     Download     Resources     Contact us    
Browse thread
Lazy modules
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Alain Frisch <alain@f...>
Subject: Re: [Caml-list] Lazy modules
On 17/03/2010 18:04, Dario Teixeira wrote:
> In a sense I need a lazy module.  Also, note that I find the solution
> of declaring all values in that module as lazy a bit inelegant.
>
> I do have a solution which though hackish and relying on a language
> extension (local modules) seems to work: I create the module on demand
> via a functor parameterised over an empty sig:

Here is a variant of your version with first-class modules (which will 
be available in OCaml 3.12). Compared to your version, we get the 
benefit of laziness (the "functor body" is evaluated only once).


module Config =
   struct
     let x = ref "XXX"
   end

module type S =
   sig
     val x: string
   end

let lazy_module =
   lazy
     (
      let module M =
        struct
          let x = !Config.x
        end
      in
      (module M : S)
     )

let foo () =
   let module M = (val Lazy.force lazy_module : S) in
   M.x


-- Alain