From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id IAA20210; Wed, 16 Oct 2002 08:43:05 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id IAA20055 for ; Wed, 16 Oct 2002 08:43:04 +0200 (MET DST) Received: from nef.ens.fr (nef.ens.fr [129.199.96.32]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g9G6h3D22241 for ; Wed, 16 Oct 2002 08:43:03 +0200 (MET DST) Received: from clipper.ens.fr (clipper-gw.ens.fr [129.199.1.22]) by nef.ens.fr (8.10.1/1.01.28121999) with ESMTP id g9G6h0B36836 ; Wed, 16 Oct 2002 08:43:00 +0200 (CEST) Received: from localhost (frisch@localhost) by clipper.ens.fr (8.12.3/jb-1.1) id g9G6gxNm027995 ; Wed, 16 Oct 2002 08:42:59 +0200 (MET DST) Date: Wed, 16 Oct 2002 08:42:59 +0200 (MET DST) From: Alain Frisch To: Chris Hecker cc: caml-list@inria.fr Subject: [Caml-list] Local open (was: monomorphic restriction or typing/scanf bug?) In-Reply-To: <4.3.2.7.2.20021015183630.0345cb18@mail.d6.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-milter (http://amavis.org/) Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Tue, 15 Oct 2002, Chris Hecker wrote: > Of course, as that thread says, "let exception Blah" would be nice, or > "local open" to make this pattern nicer to work with. cf http://www.eleves.ens.fr/home/frisch/soft#openin With the syntax extension below, you can do: glouglou ~/openin $ ocaml camlp4o.cma pa_openin.cmo Objective Caml version 3.06 Camlp4 Parsing version 3.06 # let () = open Unix in ();; # let () = struct exception E end in raise E;; Exception: E. That is: local open and local structure items. Of course, you get ugly error messages when trying to have a type escape its scope: # let () = struct type t = A end in A;; This `let module' expression has type OPENIN_5.t In this type, the locally bound module name OPENIN_5 escapes its scope Here is the code of the syntax extension: let no = ref 0 let local_struct loc st e = incr no; let x = "OPENIN_" ^ (string_of_int !no) in let st = st @ [<:str_item< value res = $e$ >>] in <:expr< let module $x$ = struct $list: st$ end in ($uid:x$.res) >> EXTEND GLOBAL: Pcaml.expr; Pcaml.expr: LEVEL "expr1" [ [ "open"; i = LIST1 UIDENT SEP "."; "in"; e = Pcaml.expr LEVEL "top" -> local_struct loc [<:str_item< open $i$ >>] e | "struct"; st = LIST0 [ s = Pcaml.str_item; OPT ";;" -> s ]; "end"; "in"; e = Pcaml.expr LEVEL "top" -> local_struct loc st e ] ]; END -- Alain ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners