From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 3EBEABC57 for ; Wed, 17 Mar 2010 18:36:37 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgkCAOyvoEvB/Bd4k2dsb2JhbACDC5d9FQEBAQEJCQoJEwMfqhSQPIEsgmBqBA X-IronPort-AV: E=Sophos;i="4.49,658,1262559600"; d="scan'208";a="47040691" Received: from smtp-msa-out01.orange.fr ([193.252.23.120]) by mail3-smtp-sop.national.inria.fr with ESMTP; 17 Mar 2010 18:36:36 +0100 Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf5a03.orange.fr (SMTP Server) with ESMTP id 710ED1C007DB; Wed, 17 Mar 2010 18:36:36 +0100 (CET) Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf5a03.orange.fr (SMTP Server) with ESMTP id 607861C001F6; Wed, 17 Mar 2010 18:36:36 +0100 (CET) Received: from [192.168.1.53] (APuteaux-154-1-29-97.w83-199.abo.wanadoo.fr [83.199.84.97]) by mwinf5a03.orange.fr (SMTP Server) with ESMTP id 0E19C1C007DB; Wed, 17 Mar 2010 18:36:36 +0100 (CET) X-ME-UUID: 20100317173636578.0E19C1C007DB@mwinf5a03.orange.fr X-ME-User-Auth: lexifi Message-ID: <4BA1131D.10208@frisch.fr> Date: Wed, 17 Mar 2010 18:36:29 +0100 From: Alain Frisch User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.8) Gecko/20100227 Thunderbird/3.0.3 MIME-Version: 1.0 To: Dario Teixeira Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Lazy modules References: <571731.77500.qm@web111510.mail.gq1.yahoo.com> In-Reply-To: <571731.77500.qm@web111510.mail.gq1.yahoo.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; frisch:01 frisch:01 functor:01 sig:01 ocaml:01 functor:01 struct:01 sig:01 val:01 struct:01 foo:01 val:01 config:01 config:01 wrote:01 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