logoalt Hacker News

.NET (OK, C#) gets union types

81 pointsby ingveyesterday at 12:28 PM63 commentsview on HN

Comments

jdw64today at 10:21 PM

C# is my strongest and favorite language. That said, it's frustrating that the C# framework ecosystem lacks solid options. MAUI is especially half-baked, and I'm really starting to doubt whether I should continue using XAML

show 2 replies
glimshetoday at 9:40 PM

I love C# and in every iteration we're getting more and more features to get C-like performance in a lot of scenarios. C# does it really well because if your problem isn't performance/memory-constrained, you can ignore these features and fallback on the language's natural ease of use.

munchlertoday at 9:10 PM

F# leads the way and C# slowly catches up, as always. Yet for some reason, C# still gets all the mindshare.

show 2 replies
hahn-kevtoday at 9:10 PM

I'm glad to finally see this making it's way into C#. Not so much because I want to use unions purely in C#. But because I want to be able to define them when interfacing with other languages.

moomintoday at 9:13 PM

AFAICT, this means you won’t be able to define Either<string, string>, which is definitely a thing you sometimes want to do.

show 3 replies
rohitsriramtoday at 9:51 PM

F# has had this for decades, C# is basically just slowly becoming F# with a C-style syntax. Not complaining though, most teams aren't switching languages so getting these features where people actually work is better than nothing.

deadeyetoday at 9:49 PM

I wish the syntax looked more like typescript. This will confuse my eyes for a while.

Quarrelsometoday at 9:11 PM

I mean yes, but also: uh-oh. I'm looking forward to reading some code that is even more confusing than the code I'm already reading.

Not entirely convinced that I see the usecase that makes up for the potential madness.

-------

EDIT: Ok so I just asked Gipity instead of wasting time chatting to smug fucks in the comments who are mostly bad at expressing themselves or only show up to crow. Thanks to everyone that did try to be helpful and I apologise for sweeping you up in that same wide brush.

So I figure its a means of better expressing branching logic that is happy path adjacent. That makes a lot of sense to me. e.g.

    return loginResult switch
    {
        LoginSucceeded success =>
            StartSession(success.User),

        InvalidCredentials =>
            ShowInvalidLoginMessage(),

        MfaRequired mfa =>
            RedirectToMfa(mfa.ChallengeId),

        AccountLocked locked =>
            ShowLockedAccountPage(locked.UnlockAt),

        PasswordExpired =>
            RedirectToPasswordReset()
    };
Where previously you'd have issues around return types without unions (e.g. mfa.ChallengeId).

I mean you could still do this with exceptions but for MFA for example that doesn't seem right.

A 500 error would still get caught via an exception thus preserving the usefulness of moving unexpected error logic to some place else and keeping the happy branch clean.

So it seems very useful. If you got any other good usecases for using it elsewhere, please let me know.

show 7 replies
le-marktoday at 9:15 PM

I used to see some excitement around .net core several years ago. I haven’t heard or seen much in the wild. Is anyone using .net on systems other than windows nowadays?

show 7 replies