简体   繁体   English

在理解 F# 中的 class 构造函数时遇到问题

[英]Having issues understanding class constructors in F#

I have the following code:我有以下代码:

module ExchangeSocket =模块 ExchangeSocket =

type Socket(url, id, key) =

    let Communicator = new TestCommunicator(url)
    let Client = new WebsocketClient(Communicator)

    do
        Communicator.Name <- "test"
        Communicator.ReconnectTimeoutMs <- int (TimeSpan.FromSeconds(30.).TotalMilliseconds)

if we look at the last two lines, the C# usage is like that:如果我们看最后两行,C# 的用法是这样的:

Communicator = new WebsocketCommunicator(wsUrl) { Name = "tst", ReconnectTimeoutMs = (int) TimeSpan.FromSeconds(30).TotalMilliseconds };

Now I read that to make a class constructor, I have to use the 'new' keyword;现在我读到要创建一个 class 构造函数,我必须使用“新”关键字; so I make the fields members and do a 'new' section:所以我让字段成员并做一个“新”部分:

    member this.communicator : TestCommunicator
    member this.client : WebsocketClient

    new() =
        this.communicator <- new TestCommunicator(url)
        this.client <- new WebsocketClient(this.communicator)

but this doesn't work (line 15 is the top line in this example)但这不起作用(第 15 行是本示例中的第一行)

Socket.fs(15, 64): [FS0010] Incomplete structured construct at or before this point in member definition. Socket.fs(15, 64): [FS0010] 成员定义中在此点或之前的不完整结构化构造。 Expected 'with', '=' or other token.应为“with”、“=”或其他标记。

my questions are:我的问题是:

  • How to make this work?如何使这项工作?
  • What does 'new' bring that 'do' doesn't? “新”带来了什么,而“做”却没有?

Now I read that to make a class constructor, I have to use the 'new' keyword现在我读到要创建一个 class 构造函数,我必须使用“新”关键字

To make additional class constructors you should use the new keyword.要制作额外的 class 构造函数,您应该使用new关键字。 The primary constructor is sufficient for you here.这里的主构造函数对你来说就足够了。

In member this.communicator: TestCommunicator , while you have specified the type of the property communicator , you have not specified how to get (or set) this property.member this.communicator: TestCommunicator中,虽然您已指定属性communicator的类型,但您尚未指定如何获取(或设置)此属性。 It is missing =... or with get/set as the error message says.它缺少=...或错误消息中的with get/set

new() =
    this.communicator <- new TestCommunicator(url)
    this.client <- new WebsocketClient(this.communicator)

When you fix the previous error, you will get another error here because 1. new() should return a Socket , and 2. it references this and url , which are not in scope.当你修复前面的错误时,你会在这里得到另一个错误,因为 1. new()应该返回一个Socket ,并且 2. 它引用thisurl ,它们不在 scope 中。

Probably what you want is:可能你想要的是:

type Socket(url, id, key) =
    let communicator =
        new TestCommunicator(url,
            Name = "test",
            ReconnectTimeoutMs = int (TimeSpan.FromSeconds(30.).TotalMilliseconds))
    let client = new WebsocketClient(communicator)
    member _.Communicator = communicator
    member _.Client = client

While this answer does what you asked for, better would be to read the spec for classes ( See https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/classes and https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/members/ ) and some examples of usage.虽然此答案符合您的要求,但最好阅读类规范(请参阅https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/classeshttps://docs。 microsoft.com/en-us/dotnet/fsharp/language-reference/members/ )和一些使用示例。 Your code suggests you were guessing which is not the right approach.您的代码表明您在猜测这不是正确的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM