There is no way to get functions like this automatically, though I believe this would be very useful. There has been some discussion about having syntax for representing property getter (e.g. String.Length instead of (fun s -> s.Length)), so I think similar feature for records would be also great.

If you're owner of the type, you can write this explicitly yourself:

1
2
3
4
5
 
type R = 
  { N : int }   
  static member getN r = r.N
  static member setN r n = { r with N = n } 

...but I guess this isn't very helpful answer. It would be great if the compiler did this automatically.

By on 1/24/2010 12:38 PM ()

...but I guess this isn't very helpful answer. It would be great if the compiler did this automatically.

You guess right :P

Just kidding: it would really be great to make F# even smarter about this kind of thing, because it would make using objects in a functional context even smoother.

Hear us, Don? :)

By on 1/25/2010 1:13 AM ()

I don't think I understand why/when you would need this. Records are useful because they have very well-understood behavior. If you need more custom behavior, then use a constructed class. Connecting the setter with the record-copy behavior just "doesn't feel right" (to bastardize Guido van Rossum).

EDIT: Ignore this post. I just went and re-read the original. I thought you were suggesting the compiler auto-generate getter/setters on the instance -- that would be weird. However, defining something on the type seems like it could be useful in some cases.

P.S. Why doesn't the forum ever let me delete my own posts?

By on 1/25/2010 6:24 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