The fact that they've added all of this great stuff and have STILL not added real discriminated unions is a damn travesty. It would just so drastically improve the language.
It's missing the killer feature though, which is the compiler warning you when you haven't done an exhaustive match. That is the magic which makes adding new values easy instead of hunting through code to find if you missed the new case anywhere.
I used to think that.... but actually, in practice, and I've been using them for a long time now, it doesn't make much difference, good tooling will generate the cases and help you find all instances pretty quick (I use Rider).
I've only needed discriminated unions inside C# when I'm using pinvoke to C DLLs. I would think Rosylin supports some compiler warning for them, but I haven't checked that.
This is the kind of OG way to do it even before we had patterns. There are a couple of problems with it, least of which is that I can't then use one or both of those in a wider union elsewhere, because their definitions are bound to that parent class. Ideally I'd like to see something very similar to the linked OneOf library. That allows you to do both in-line definitions OR subclass from OneOf<T1,T2,T3...> to reify the union as a class as well. If that is done and integrated properly into the pattern matching system, I believe it will yield very powerful expressiveness.
Is is kind of a pain particularly when working across Typescript projects. OneOf is cool, but it DOES NOT work well with null and thus optional parameters.
Given few people anticipated ValueTuple and C# adding a more direct tuple syntax, I feel like it is only a matter of time before C# adds discriminated unions.
One of the problems here is that C# and F# interop isn't always as easy as the article implies. F# has a lot of types which, when exposed in a public API, are ugly as sin in other .NET languages.
That issue is not F# or C#'s fault - it's a limitation of the expressiveness of the CLR's type-system (it doesn't support higher-kinded types, varadic type parameterization, or non-typename type parameters).
The hope and the expectation is that the CLR will gain support for first-class representations of F# concepts to allow for greater interop with C# scenarios, but the CLR's development has always been tied to C#, with other CLR languages like VB.NET and C++/CLI only exerting minor influence on the CLR's design with most of their language-specific idiosyncrasies being handled by library-code and compile-time tricks instead (e.g. VB.NET's "On Error Resume Next" statement is implemented by having the compiler wrap each individual statement in a try/catch instead of having the CLR specifically support it (though in this specific case that's probably a good idea as OnErrorResumeNext is a horrible idea I'm sure we all agree).
Minor correction, only Managed C++ and C++/CLI have the full power of the CLI, many of the performance improvements in C# have been related to exposing those MSIL capabilities to C# as well, which before required generating bytecode directly.
Which is kind of ironic given how they usually leave C++/CLI out of the picture, including the cross platform story.
Did you check the recents blog posts about C#12? Clearly the team has ran out of ideas for meaningful improvements to the language, while ignoring discriminated unions which is the most important concept missing in the language.
I’m not arguing there is more important stuff, but DUs would enhance the language on a wide, fundamental level, accelerating a lot of other advancements.
The only thing I really missed, has been adopted during the latest years, pattern matching.
Languages are not used in isolation, great IDE experience, and having mature libraries for every use I can think of, is more valuable than grammar and semantics.
Also a reason why I would rather do FP in C++23 than Haskell, even with all the warts and paper cuts it entails, ecosystem.
If only the tooling was at the same level as C# and Java.
Additionally, they add friction to a development stack, now everyone needs to be confortable with two language stacks, and most of the time it isn't really worth it.
its because they are easy to do in C# without explicit support, but the proposal is still in the works, but they argue a lot about the syntax and about exhaustive type checking for all the edge cases.
FWIW, this a switch expression rather than a switch statement.
But in any case I really love this addition to the language but the inability to have multi-line or block expression arms is a constant annoyance for me.
You can even combine these with the new one line record syntax to create a poor man’s discriminated union.
Your pedantic correction is actually important for another reason: the switch expression (unlike the switch statement) is defined at the language level as an expression (evaluating to/“returning” a value), which would be ok except C# doesn’t (at the language level) have a void/unit type, meaning the switch statement has to return an actual value, limiting the places you can use it compared to the F# counterpart (or the match expression from rust, etc) to very specific cases, usually those performing an assignment.
The workaround for that is the same as the workaround for the really lame one line limitation: you need to call a (preferably (static) local) function in the handler portion and then return something like `true` assigned to a discard. Hacks all around!
Eg
_ = foo switch a when … => CaseA(foo),
_ => CaseB(foo);
With CaseA and CaseB returning bool in order to call a function depending on the value of foo rather than assign a value.
The blog is indeed the worst example you could run for this case. As you pointed out, modern C# would be same length (which is his "wow" effect on the functional core) but also has the F# not really a higher readability than the shown C# code.
Eg: https://sharplab.io/#v2:EYLgZgpghgLgrgJwgZwLQAUEEsC2UECeAwgP...