我想在 F# 中定义一个重载的成员函数
member o.m1(?x: int) =
o.m1("bugs bunny", x) // <- error, expects a straight int, but x is int option
member o.m1(s: string, ?x: int) =
42
但是上面的代码失败了。我可以这样解决:
member o.m1(?x: int) =
match x with
| Some x -> o.m1("bugs bunny", x)
| _ -> o.m1("bugs bunny")
我想知道是否有可能避免这种转换。
最佳答案
您可以通过显式命名可选参数来实现,如下所示:
member o.m1(?x: int) =
o.m1("bugs bunny", ?x = x)
关于f# - 是否可以在 F# 中转发可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72523914/