如何通过在调用站点中指定参数名称来调用 F# 函数?
我试过以下方法:
let add x y =
x + y
add (x = 10) (y = 10) // How to specify the name x and y when calling add?
但它给出了这个错误:
error FS0039: The value or constructor 'x' is not defined.
最佳答案
您不能使用命名参数调用 let
绑定(bind)函数。它只允许用于类中的方法
Named arguments are allowed only for methods, not for let-bound functions, function values, or lambda expressions.
从技术上讲,您可以声明静态类并使用其中的方法,但我认为这是错误的。只是错了。不要这样做
[<AbstractClass; Sealed>]
type MathOperations =
static member Add (x, y) = x + y
open type MathOperations
[<EntryPoint>]
let main argv =
Add(x = 3, y = 4)
|> printfn "%d"
0
关于f# - 如何在 F# 中使用命名参数调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67455161/