[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | David Allsopp <dra-news@m...> |
| Subject: | let rec and polymorphic functions |
Why is let rec apparently unable to infer polymorphic function types? In
both the expressions below, I'd expect [out] to have type [('a, unit,
string, unit) format4 -> 'a]. Why when used in a [let rec] construct is it
clearly inferred as [('_a, unit, string, unit) format4 -> '_a] and then
instantiated as [(unit, unit, string, unit) format4 -> unit] by the first
call to [out] in [f]? It seems to contradict the end of Section 6.7.1 of the
manual.
I know that [out] and [f] are not mutually recursive so there's no need to
use [let rec] but I tend to use [let rec] in situations where I'm defining
two functions where one uses the other at the [let ... in] level as it saves
writing the extra [in]! This appears potentially to be a mistake, though...
As ever, a technical explanation of why the type system behaves this way
much appreciated! I won't make judgement on the hours of time wasted by the
cryptic type errors in this case ;o)
Just in case it matters, I'm using O'Caml 3.09.3...
David
(*
* This first example works.
*)
let out line =
Printf.printf line
in
let f () =
(*
* [out] is clearly polymorphic
*)
out "TEST";
out "%d" 0;
out "%b" false;
in
f ();;
(*
* This second example does not. Why?
*)
let rec out line =
Printf.printf line
and f () =
(*
* [out] gets inferred as string -> unit here...
*)
out "TEST";
(*
* ... and so we get a "too many parameters" error here.
*)
out "%d" 0;
in
f ();;