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-15 (03:52)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] monomorphic restriction or typing/scanf bug?
From: Chris Hecker <>

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

This is a known issue: named type variables inside an expression are
restricted to be monomorphic for the whole toplevel expression.  They
can only be generalized afterwards. (This is to ensure that two
identical variable names in an expression refer to the same variable)
The standard workaround is to use nameless ones.

> (* this works *)
> let test2 () =
>   let line = "Foo 1" in
>   let scan s (fmt : (_, Scanf.Scanning.scanbuf, _) 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

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