【问题标题】:F# extensions used in a context where a function is not expected在不需要函数的上下文中使用 F# 扩展
【发布时间】:2019-06-06 06:57:42
【问题描述】:

您好,我在 f# 中有一个扩展方法,用于检查 TcpClinet 是否已连接,它是相当简单的代码:

[<Extension>]
type TcpExtension() =
    [<Extension>]
    static member inline IsConnectionEstablished(client: TcpClient) : bool = 
       let ipProperties : IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties()
       let tcpConnection : Option<TcpConnectionInformation> = ipProperties.GetActiveTcpConnections() |> Seq.tryFind(fun connection -> connection.LocalEndPoint.Equals(client.Client.LocalEndPoint) && connection.RemoteEndPoint.Equals(client.Client.RemoteEndPoint))

       match tcpConnection with
         | Some connection -> true
         | None -> false

现在我尝试简单地使用它:

let private WaitForData (client : TcpClient) = 
    let isConnectionAlive : bool = client.IsConnectionEstablished
    isConnectionAlive

但我收到的消息如下:

这个函数接受了太多参数,或者在不期望函数的上下文中使用

当我检查 Microsoft 文档时,这是他们显示的处理方式,我在这里遗漏了什么?

【问题讨论】:

    标签: f#


    【解决方案1】:

    错误信息有点误导,你实际上缺少一个参数

    要调用函数,您需要传入 unit,就像从 C# 调用它一样

    let isConnectionAlive : bool = client.IsConnectionEstablished()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      • 2017-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多