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: -- (:)
From: Chris Hecker <checker@d...>
Subject: Re: [Caml-list] monomorphic restriction or typing/scanf bug?

[Brian sent this to me privately, but it was a good point I forgot about, 
so I'm replying publicly.]

At 14:31 10/15/2002 -0700, wrote:
>On Tue, 15 Oct 2002, Chris Hecker wrote:
> > Now, if only we could have local exceptions to make those "C break-like
> > exception patterns" easier!
>Why do the exceptions need anything more than module locality, especially
>when function locality can be achieved with local modules?

Ack, I forgot about that solution, and forgot the specifics of the fact 
that I asked about this and there was a thread about it over a year ago:

I actually didn't forget that I asked, but for some reason I thought the 
restrictions on types escaping the local modules applied to exceptions as 
well.  Oops!  Thanks for pointing it out!

Of course, as that thread says, "let exception Blah" would be nice, or 
"local open" to make this pattern nicer to work with.

Anyway, using all of the ideas from this thread leads me to the following 
simple answer to my earlier post (which is closely related to the earlier 
post about scanf in pattern matching):

type t =
     Foo of int
   | Bar of int * int

let test line =
   let module Scan = struct
     exception Result of t
     let scan s fmt f =
         raise (Result (Scanf.sscanf s fmt f))
         End_of_file | Scanf.Scan_failure _ -> ()
   end in
       Scan.scan line "Foo %d" (fun i -> Foo i);
       Scan.scan line "Bar %d %d" (fun i j -> Bar (i,j));
       failwith "bad line"
       Scan.Result t -> t

# test "Foo 1";;
- : t = Foo 1
# test "Bar 1 2";;
- : t = Bar (1, 2)
# test "Bar 1";;
Exception: Failure "bad line".


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