Multi constructor for classes in F#

When we define an F# class in explicit way, it's easy for new F# programmers to make this kind of mistakes when they define mult constructors: people will forget to initliza the class first before use it.

type myCls() = 
    //This this a new feature in VS2012,define a property is much easier than before.
    member val Value = 0 with get,set

    member this.Print() =
        printfn "%d" this.Value

    //Must initilaize the second construct by the default one before set the property Value
    new(value : int) as this = 
        new myCls()
        then
            this.Value <- value

 

  

posted @ 2012-11-07 14:32  ZackZhou  阅读(154)  评论(0编辑  收藏  举报