The Elephant in the Room + Wish List — Syntax of Discriminated Unions #4229
Replies: 1 comment 10 replies
-
This is in the working set for C# 10.0 so I expect that there will be quite a few discussions about the behavior and syntax of DUs coming out of language design meetings early in the next year. I'd suggest that a great deal about the design of DUs still must be hammered out, including their implementation details. For instance you suggest that DUs might be based on abstract classes. That might be the case, but I'd suggest that requiring another allocation for a data container around your data is pretty expensive, especially for common DUs like However, if it has to be about syntax, I would probably go with something like the following: public struct enum Maybe<T> {
Some(T Value),
None
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
DUs are expected to be the flagship feature of the next C# version.
However, I am under the impression to have heard surprisingly little about DUs considering that they will come out next year (compared to records, which were in discussion since 2014, IIRC).
I expect DUs to be implemented as abstract classes/records with private constructors. Each DU case would be a nested class of the DU and implement the parent DU. The nesting gives them exclusive access to the private base constructor.
This would be comparable to my own version of DUs:
I use T4-Templates to generate these kind of classes:
These are used as follows:
My solution is far from ideal. I absolutely adore the F# syntax, but this would be impossible to adapt identically into C#.
So, my question is: What could the syntax of future DUs look like?
I am under the (probable wrong) impression, that the DU's future syntax has not yet really been addressed by the LDT, which is the reason why I will use the clickbait-headline of "the elephant in the room".
I also want this discussion to be like a Christmas whish list:
Please to comment which syntaxes you want to see for DUs!
Beta Was this translation helpful? Give feedback.
All reactions