From 1.9.2.7 you can use the following:

type Person =
{ First: string; Last: string }
static member make (?first, ?last) =
{ First = defaultArg first "John"; Last = defaultArg last "Doe" }

By on 9/3/2008 5:41 PM ()

I like felixmar's suggestion. It's a fairly obvious extension to suggest that type inference could reduce this to the beautifully succinct:

1
2
3
type Person =
    { First : "John" ;
      Last  : "Doe" }

I remember Don mentioning that there were discussions about unifying syntax for records and classes. I would love to see a property syntax for classes something like:

1
2
3
4
5
type Person =
    First : mutable "John" 
    Last  : mutable "Doe" 

    override x.ToString() = x.First + " " + x.Second

I do hate superfluous brackets and braces! (One of the many reasons I love F#.)

Currently this class would look so:

1
2
3
4
5
6
 type Person(?first, ?second) =
    let mutable first = defaultArg first "John"
    let mutable second = defaultArg second "Doe"
    member this.First with get () = first and set v = first <- v
    member this.Second with get () = second and set v = second <- v
    override x.ToString() = first + " " + second

which somewhat obscures the nature of the class.

regards,

Danny

By on 9/4/2008 3:57 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper