32

For the normal property getter/setter syntax

let mutable myInternalValue

member this.MyProperty 
    with get() = myInternalValue
    and set(value) = myInternalValue <- value

is there a shortcut, similar to the following in C#?

someType MyProperty { get; set; }

If there is one, I seem to be unable to find it...

3 Answers 3

61

F# 3 has auto-implemented properties so you can declare properties without declaring the backing field.

Example taken from Properties(F#) on MSDN:

type MyClass() =
    member val MyProperty = "" with get, set
1
  • 3
    Note the syntax for abstract properties, i.e. properties declared by an interface, is completely different using abstract as a prefix keyword, omitting the val keyword, but optionally retaining the member keyword... Feb 9, 2018 at 12:23
12

There is no shortcut for creating get/set property in F#, but if you want a property with just get you can use the following simple syntax:

type Test(n) =
  let myValue = n * 2
  member x.Property = 
    // Evaluated each time you access property value
    myValue + 5

Since F# is a functional language, F# types are very often immutable. This syntax makes it very easy to define immutable types, so it also encourages you to use a good functional programming style in F#.

However, I agree that a shortcut for get/set property would be very useful, especially when writing some code that needs to interoperate with C# / other .NET libraries.

EDIT (5 years later...): This has been added in F# 3.0, as the answer from Ben shows :-)

6
  • So you say it's an educational measure? What I think is a bit tedious (and was in the first versions of C#) is the need to write the internal value explicitly, and the short C# syntax also makes it easy to have getters/setters with different access modifiers. Not saying I can't live with the current state of affairs, it's a minor point, really. Oct 12, 2010 at 10:42
  • @Alexander: I wouldn't really say that the motivation for not having easy syntax for get/set properties is educational. It's probably a matter of priorities for the F# team. Adding support for mutable properties would be more work, but I agree it would be really useful. Oct 12, 2010 at 13:14
  • 3
    @Alexander--not quite sure of the scenario but as I see more and more code where people mindlessly add getters and setters (or inspectors and mutators as I've sometimes heard them call) that do nothing other than return the value or change it, it's harder and harder to justify not just making the values public members. I mean code that doesn't hide the type behind some typedef and simply reads a member from a class and simply sets the value in the class might just as well be public--it's coupled just the same after all. Maybe it's a good thing that F# forces you to add this if you want it. Oct 12, 2010 at 13:27
  • 1
    An alternative option is to create a local reference cell and expose it using member. let local = ref 10 and member x.Prop = local (note that you need two lines - because member body is evaluated each time it is used). But .NET properties are certainly more idiomatic. Oct 12, 2010 at 14:41
  • 1
    @Onorio Catenacci: simple getters and setters rather than public fields are often necessary in order to benefit from things like ASP.NET / WPF data-binding and XML serialization. C# 3.0 eased the boiler-plate required for these scenarios with Auto-Implemented Properties. Oct 12, 2010 at 20:25
2

is there a shortcut, similar to the following in C#?

I don't think so (nothing shown in "Expert F#"'s syntax summary), and the F# syntax is already quite brief compared to the full syntax needed in C#.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.