Hacker News new | ask | show | jobs
by mmusc 12 days ago
https://learn.microsoft.com/en-us/dotnet/csharp/language-ref...

they're coming

1 comments

Yeah, I still don't understand why they don't just use enums and have to invent a special "union" keyword instead. C#'s enums are extremely underpowered to begin with and the syntax they designed here way more convoluted and uglier to say:

    enum Pet { 
    Cat,
    Dog,
    Bird
    }

    var description = Pet switch
    {
        Dog d => d.Name,
        Cat c => c.Name,
        Bird b => b.Name,
        _ => "no pet"
    }
The other issue is they introduced `null` to pattern matching on their union example when the whole point of pattern matching is that you can pretty much mostly remove `null` from the language altogether with Result<T, E> available, as Rust has shown.
It sounds like they didn't reuse the enum keyword for this for the most backwards compatibility and to avoid confusion. Your example enum is an `int` today. Either you break existing code expecting an `int` sized thing or you need additional syntax and can't use the "clean" form that you are hoping for.

The null patterns predate these unions and the relevant section of the documentation is showing that you can use nullable unions, not that you have to or should use them. Other examples also show a fully closed Result<T, E> type that yeah would be great for projects hoping to eliminate more nullable code.