Question

Firstly, is the F# 3.1 spec available online? If so, the answer for this should be easy enough to find.

I'm wondering if the compiled form of discriminated unions with named fields (added in 3.1) will include properties with the names of the fields, instead of the usual Item1, Item2, ... properties.

Was it helpful?

Solution

As far as I know, the specification is not available yet, so the only way to tell is to try it. I installed VS 2013 RC, so I thought I could check using this union:

type Expression = 
  | Add of Left:Expression * Right:Expression
  | Constant of number:int

As expected, it does generate named properties, which are nicely useable from C#:

enter image description here

In fact, it also uses the name in parameters of the construction functions:

enter image description here

If you're using them for C# interoperability, then you'll probably want to write the union member names in PascalCase, because the compiler does not automatically capitalize the name of the property if you use lowercase name (but interestingly, it does use a lowercase name for the parameters).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top