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
Lazy modules
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-03-17 (17:04)
From: Dario Teixeira <darioteixeira@y...>
Subject: Lazy modules

I've come across a problem which though novel to me, I presume must be
familiar to those with more ML baggage.  Namely, I need a module whose
values are not known at the initialisation stage, since they can only
be determined after reading a configuration file.  If this were a lone
value, I would declare it as a lazy value which when forced would read
from the configuration file.  But how can I achieve similar semantics
with a whole module? 

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:

module Socket_maker (S: sig end) : Client.SOCKET =
    let sockaddr = !Config.sockaddr
    let sockdomain = !Config.sockdomain
    let socktype = !Config.socktype
    let sockproto = !Config.sockproto

let foo =
    let module Socket = Socket_maker (struct end) in

But I wonder a) what's the penalty associated with the runtime application
of the functor, and b) if there's some better way of doing this.  Any thoughts?

Thanks for your time!
Best regards,
Dario Teixeira