我刚刚开始使用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) 让我们上这堂课:
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)