有没有办法在辅助构造函数中设置自动属性?

use*_*480 9 f#

我有这门课:

type Sample() =
    member val test1 = "" with get,set
    member val test2 = "" with get,set

    // is something like the below constructor possible
    new Sample(result1, result2) =
        this.test1 <- "failed"
        this.test2 <- "passed"
        Sample()
Run Code Online (Sandbox Code Playgroud)

我尝试了几种不同的方法,但我无法让它发挥作用.

Mar*_*ann 6

这是你想要的吗?

type Sample(result1, result2) =
    member val Test1 = result1 with get,set
    member val Test2 = result2 with get,set
    new () = Sample("failed", "passed")
Run Code Online (Sandbox Code Playgroud)

FSI:

> Sample();;
val it : Sample = FSI_0002+Sample {Test1 = "failed";
                                   Test2 = "passed";}
> Sample("foo", "bar");;
val it : Sample = FSI_0002+Sample {Test1 = "foo";
                                   Test2 = "bar";}
Run Code Online (Sandbox Code Playgroud)


scr*_*wtp 5

@Mark Seemann的答案是正确的解决方案,但你可以使用这种奇怪的结构得到你想要的东西:

type Sample() =
    member val test1 = "" with get,set
    member val test2 = "" with get,set

    new (result1, result2) as sample =
        Sample()
            then
                sample.test1 <- result1
                sample.test2 <- result2
Run Code Online (Sandbox Code Playgroud)

但事实上,这是我从未使用过的东西,可能从未在野外使用过,而且更像是语言琐事,而不是其他任何东西.