문제

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.

도움이 되었습니까?

해결책

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).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top