相关疑难解决方法(0)

对象初始化语法

我刚刚开始使用F#,我找不到像C#3那样进行对象初始化的语法.

即这样:

public class Person {
  public DateTime BirthDate { get; set; }
  public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

如何在F#中编写以下内容:

var p = new Person { Name = "John", BirthDate = DateTime.Now };
Run Code Online (Sandbox Code Playgroud)

syntax f# object-initializers

95
推荐指数
3
解决办法
2万
查看次数

对 F# 中的字段及其访问规则以及初始化非常困惑

让我们上这堂课:

type Test() =

    let mutable Flag1 : bool = false
    [<DefaultValue>] val mutable Flag2 : bool

    do
        Flag1 <- true  // that works
        Flag2 <- true  // nope... why?

    member this.SetFlag =
        Flag1 <- true  // no 'this' instance? does that mean it's static?
        this.Flag2 <- true  // that works now, but still no way to set a default value
Run Code Online (Sandbox Code Playgroud)

[<DefaultValue>]当我希望能够将值设置为我自己的任何值时,为什么我需要?

然后现在:

type Test2() =
    Inherit(Test)

    do
        Flag1 <- true  // not accessible
        Flag2 <- true  // not …
Run Code Online (Sandbox Code Playgroud)

f#

5
推荐指数
1
解决办法
91
查看次数

标签 统计

f# ×2

object-initializers ×1

syntax ×1