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
[Caml-list] monomorphic restriction or typing/scanf bug?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-10-16 (06:43)
From: Alain Frisch <frisch@c...>
Subject: [Caml-list] Local open (was: monomorphic restriction or typing/scanf bug?)
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.


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) >>

 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";
          e = Pcaml.expr LEVEL "top" ->
            local_struct loc st e


-- Alain

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: