The key is that labels do not exist at runtime. A function of type X:int -> y:float -> int
is really a function whose first argument is an int and whose second argument is a float.
Calling g ~y:123
means that we store the second argument 123 somewhere (in a closure) and we will use it automatically later when the original function g
is finally called with all its arguments.
Now consider a higher-order function such as foobar
:
let foobar (f : y:float -> x:int -> int) = f ~x:1 ~y:2. (* which is the same as: *) let foobar (f : y:float -> x:int -> int) = f 2. 1
The function f
passed to foobar
takes two arguments, and the float must be the first argument, at runtime.
Maybe it would be possible to support your wish, but it would add some overhead. In order for the following to work:
let g ~x ~y = x + truncate y;; foobar g (* rejected *)
the compiler would have to create an extra closure. Instead you are required to do it yourself, as follows:
let g ~x ~y = x + truncate y;; foobar (fun ~y ~x -> g ~x ~y)
In general, the OCaml compiler is very straightforward and won't perform this kind of hard-to-guess code insertion for you.
(I'm not a type theorist either)