Version française
Home     About     Download     Resources     Contact us    
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, brogoff@speakeasy.net 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:

http://groups.google.com/groups?th=6f10c8b9743e7f20

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 =
       try
         raise (Result (Scanf.sscanf s fmt f))
       with
         End_of_file | Scanf.Scan_failure _ -> ()
   end in
     try
       Scan.scan line "Foo %d" (fun i -> Foo i);
       Scan.scan line "Bar %d %d" (fun i j -> Bar (i,j));
       failwith "bad line"
     with
       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".

Chris


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