在构造包含类时设置类的属性

kas*_*rhj 2 f#

一类Test,其在建设创建另一个类的实例,并设置属性会是这个样子(我想):

type Test() as this =
    let a = new A()

    do this.Init()

    member this.Init() = 
        let a.Size = 10
Run Code Online (Sandbox Code Playgroud)

但是,我得到了一个 Block following this 'let' is unfinished. Expect an expression.

这样做的正确和首选方式是什么?

Mar*_*ann 5

如果要改变Size属性,则必须使用赋值运算符:

type Test() as this =
    let a = new A()

    do this.Init()

    member this.Init() = 
        a.Size <- 10
Run Code Online (Sandbox Code Playgroud)

但是,你可以像这样简洁地写它:

type Test() =
    let a = A (Size = 10)
Run Code Online (Sandbox Code Playgroud)