anonymous functions with optional arguments #5748
Original bug ID: 5748
In OCaml 4.00, anonymous functions with optional arguments can no longer be passed to a higher order function that doesn't know expect optional arguments. Here is an example:
let f g = g () ;;
(* works in 3.12 and 4.00 *)
Here is what no longer works in OCaml 4.00.0 but used to work in 3.12.1:
f (fun ?opt () -> ()) ;;
Error: This function should have type unit -> 'a
This breaks some of our code where the anonymous function is generated by a Camlp4 macro.
The text was updated successfully, but these errors were encountered:
Comment author: @garrigue
Repeating the message of the caml-list (feedback request at the end):
OCaml 4.00 is much more agressive in propagating expected types when typing
Specifically, this is this behavior:
val f : (unit -> unit) -> unit
let () = f g
This code triggers automatic discarding of the optional arguments of g,
But for this we need to first infer the type of the argument, to see that
I was not aware that this feature was widely used, and the behavior in 4.00.0
An inline function doesn't enter this category.
I'm pondering what to do about this.
Not propagating types to function arguments seems fine, but for variant and record
How much do you rely on that?
Comment author: @alainfrisch
Jacques: what do you call "upward" propagation? Propagating the expected types down the AST?
Wouldn't it be possible to trigger the erasure of an optional argument "on the fly" when reaching a lambda node, if the expected type asks for a function type without this optional argument?
That said, I'm not a big fan of the automatic erasure of optional arguments (it looks rather fragile, and not so useful).
Comment author: @mjambon
This problem occurs in two spots in a ~100K-line code base and I don't see much more need for it in the future.
The macro we're using is FILTER provided by mikmatch_pcre:
let f = FILTER int eos;;
val f : ?share:bool -> ?pos:int -> string -> bool =
List.filter (FILTER int eos) [ "-123"; "a"; "0"; "-1.2" ];;