I have a number of functions with default parameters, eg.
let h_foo a b = a * b
let foo ?(f_heuristic=h_foo) a b = f_heuristic a b
(* caller of foo where may want to change `f_heuristic` *)
let fn ?(f=foo) a b =
f a b
fn 5 6 (* => 30 *)
But, I want to be able to call the wrapper functions with different values of the defaults for the defaults of the wrapper function. I run into the following error, that confuses me, and I don't know how to resolve.
fn ~f:(fun a b -> a + b) 5 6
(* Line 1, characters 6-24:
* Error: This function should have type
* ?f_heuristic:(int -> int -> int) -> int -> int -> int
* but its first argument is not labelled *)
Is this possible in Ocaml, or is it the wrong approach? ThankS
Try this:
let fn ?(f=(foo : int -> int -> int)) a b = f a b;;
The problem is the type for the optional argument f
in your code is inferred to be the type of foo
which has an optional argument. By changing the default value to have the type you want, you can give fn
the type you want too.