I've recently become aware of the proposal to add discriminated unions to C#. Personally I think this is one of the biggest features missing from C# right now and I'd love to see it implemented.

https://github.com/dotnet/csharplang/blob/main/proposals/TypeUnions.md

#dotnet #csharp

csharplang/proposals/TypeUnions.md at main · dotnet/csharplang

The official repo for the design of the C# programming language - dotnet/csharplang

GitHub
@samwalpole Curious. I don't miss unions at all after using them for over a decade in C. They are actually often a pain with little gain, add a lot of hidden complexity that is not obvious from the start and can result in sleepless nights if not properly covered by meaningful unit tests. So what's the reason why you think they are the biggest missing features? Can you offer a meaningful example?
@maxitb @samwalpole Unions in C are very different from the “discriminated unions” this is talking about. Here’s a library that tries to add type unions to C# with some example use cases: https://github.com/mcintyre321/OneOf
GitHub - mcintyre321/OneOf: Easy to use F#-like ~discriminated~ unions for C# with exhaustive compile time matching

Easy to use F#-like ~discriminated~ unions for C# with exhaustive compile time matching - mcintyre321/OneOf

GitHub
@kleinhaus @maxitb I don't have any experience in C, but something I find them useful for is returning some data or an error. Currently I find myself creating result objects that have nullable Data and Error properties which is then quite clunky to check. The idea with unions would be to return something that either is only the data or only the error, which I think is a much more robust way of doing things.

@samwalpole @kleinhaus
I disagree, this sound more like an anti-pattern. For this scenario we have named tuples already; which would be way more efficient for this case than discriminated unions. The only difference would be a switch over Kind instead of an if statement checking nullability of the error object (in case of a integer it could be a numeric equality check).

https://sharplab.io/#v2:C4LghgzgtgPgAgZgARgEYWAJzAY2EuAJiQGEkBYAKAG8ql6Dk0Ns8kAKAe1QCsBTPAH4kAUUyZOmADRJu/NgCU+EAK4AbYAEokABQkATFTj7tNAbjoNL9RAQAspTgDtVUE5utJalBr6QA3MEwOPnFJGUxldS0kAF5dAyN3Cx8/egBLADN2UIlg9IgkJ058J3U1bU9fbzS0gHo6pAAVAHkAERbRMOCACzAnfTV0pwBzKoYAX3H6PjUIPmmvRd8G5vbOgAl+wb4kSNUNRanU+mOJoA

SharpLab

C#/VB/F# compiler playground.

@kleinhaus @samwalpole I know what they are, my questions was not what they do but:

1) So what's the reason why you think they are the biggest missing features?

2) Can you offer a meaningful example?

As stated before, I am curious about those answers in context of delivering the same code quality (which means no short cuts in ignoring execution branches).