f# - 如何在 F# 中使用命名参数调用函数

标签 f#

如何通过在调用站点中指定参数名称来调用 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.

Documentation


从技术上讲,您可以声明静态类并使用其中的方法,但我认为这是错误的。只是错了。不要这样做

[<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/

相关文章:

f# - 如何在 F# 中打印一个 bigint?

f# - F# 中的快捷等式检查?

f# - 在 F# 中记录受歧视的联合

f# - 为什么 'printf "%A"None' 输出 null

F# 错误 : This value is not a function and cannot be applied

inheritance - F# 添加多个调用基类构造函数的构造函数重载

map - f#设置和映射相等

f# - 如何编写一个函数,该函数返回数组 y 中数组 x 中值的索引数组?

visual-studio-2013 - 有没有办法在不重建项目的情况下刷新智能感知?

f# - F# 中最接近 Haskell GADT 和类型类的是什么?