Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to set automatic properties in secondary constructors?

Tags:

f#

I have this class:

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()

I've tried several different ways, but I can not get it to work.

like image 349
user1206480 Avatar asked Sep 09 '15 19:09

user1206480


1 Answers

Is this what you want?

type Sample(result1, result2) =
    member val Test1 = result1 with get,set
    member val Test2 = result2 with get,set
    new () = Sample("failed", "passed")

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";}
like image 54
Mark Seemann Avatar answered Oct 22 '22 08:10

Mark Seemann