将默认参数传递为默认参数的功能

问题描述 投票:0回答:1

我有许多带有默认参数的功能,例如。

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 *)

但是,我希望能够为包装函数的默认值使用不同的默认值来调用包装函数。我遇到以下错误,这使我感到困惑,并且我不知道如何解决。

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 *)

这在Ocaml中是否可行,还是错误的方法?谢谢]]

我有许多带有默认参数的功能,例如。 let h_foo ab = a * b let foo?(f_heuristic = h_foo)ab = f_heuristic ab(* foo的调用者可能要更改`f_heuristic` *)let fn?(...

ocaml conventions optional-parameters default-arguments
1个回答
0
投票
尝试一下:
© www.soinside.com 2019 - 2024. All rights reserved.