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: [Caml-list] monomorphic restriction or typing/scanf bug?

The "scan" function in the code below works if it's at global scope,
but not if it's defined inside the test2 function.  Is this a bug or a
typing restriction?  I assume the latter (and that I could have boiled
this example to one of the ones in the FAQ?), but I don't understand
these kinds of polymorphism typing issues.

Chris


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

exception FB of t


(* works *)
let scan s (fmt : ('a, Scanf.Scanning.scanbuf, 'b) format) f =
  try
    raise (FB (Scanf.sscanf s fmt f))
  with
    End_of_file | Scanf.Scan_failure _ -> ()

let test () =
  let line = "Foo 1" in
  try
    scan line "Foo %d" (fun i -> Foo i);
    scan line "Bar %d %d" (fun i j -> Bar (i,j));
    failwith "bad line"
  with
    FB t -> t

(* doesn't work *)
let test2 () =
  let line = "Foo 1" in
  let scan s (fmt : ('a, Scanf.Scanning.scanbuf, 'b) format) f =
    try
      raise (FB (Scanf.sscanf s fmt f));
      ()
    with
      End_of_file | Scanf.Scan_failure _ -> ()
  in
  try
    scan line "Foo %d" (fun i -> Foo i);
    scan line "Bar %d %d" (fun i j -> Bar (i,j));
    failwith "bad line"
  with
    FB t -> t

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