Hi
The F# construct

1
val mutable mass:double

is almost the same as

1
public double Mass {get;set}

in C# because F# implements member values as properties rather than fields. So if you need only trivial properties without extra logic in getter or setter you don't have to declare both val and member. The auto-generated backing field is also internal but not private - I don't know why but don't forget that the language is still being "polished".
You may also look at the relatively new feature called implicit construction:

1
2
3
type Entity (newmass : double) =
  let mutable mass = newmass
  member x.Mass with get = mass and set v = mass <- v 

However this code has some disadvantages: it causes the type Entity to contain an additional field for newmass and I suppose ther's no way to declare newmass as a mutable value.
If you do not plan your code to be consumed by other CLR languages you don't have to mimic C# with just a different syntax. F# exposes its true power when you use functional style. Look at the design of the standard library - immutable values are used very frequently and creating a new object instead of modifying existing one is usually not too much expensive. Access modifiers are usually needed to protect a value from modification and if the value is immutable you don't need to protect it.

By on 5/24/2008 1:23 PM ()

It's almost the same in structures; however, not in classes. For some reason, using the 1.9.4.17 release, in classes (and as far as I can tell, ONLY classes), the backing field is public, rather than private/internal. Because I'm consuming this library from C#, I can see myEntity.Mass, as well as the backing field myEntity._Mass. It only happens in classes, though, not structures. If I were using this class from only F#, then I'd have no problem doing it that way; however, I'm not.

By on 5/24/2008 5:58 PM ()
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