【问题标题】:How to call extension methods to pass result to an F# function如何调用扩展方法将结果传递给 F# 函数
【发布时间】:2017-02-15 22:44:51
【问题描述】:

为什么在示例 1、2、3 中我没有问题,但示例 3 给了我一个错误,上面写着:

此函数接受的参数过多,或在不期望函数的上下文中使用

let add a b =
    a + b

printfn "Example 1: %i" (add 1 2)

let append (list:seq<int>) x =
    list.Concat [x]

let count = (append [1;2] 3).Count()
printfn "Example 2: %i" count

printfn "Example 3: %i" (Enumerable.Count(append [1;2] 3))

printfn "Example 4: %i" (append [1;2] 3).Count

【问题讨论】:

  • 我怀疑你需要额外的括号(但现在没有编译器要检查)-试试((append [1;2] 3).Count)
  • 你忘了括号:(append [1;2] 3).Count()
  • Count() 是扩展方法,而不是属性,因此您需要提供 () 才能调用它。此外,您还需要将整个表达式分组在括号中:((append [1;2] 3).Count())。这是一个解释何时使用括号以及为什么在 F# 中的答案:stackoverflow.com/a/39296035/126014

标签: linq f#


【解决方案1】:

感谢马克的提示!下面的示例适用于我最初尝试做的事情。

let append (list:seq<int>) x =
    list.Concat [x]

printfn "Example 4: %i" ((append [1;2] 3).Count())

如果我有机会简要解释为什么需要额外的一组括号,我会用详细信息更新这个答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    相关资源
    最近更新 更多