logoalt Hacker News

Quarrelsometoday at 9:11 PM7 repliesview on HN

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.


Replies

zoogenytoday at 10:05 PM

This is a classic debate in programming, literally:

2001: "Beating the Averages" (Paul Graham) [1]

2006: "Can Your Programming Language Do This?" (Joel Spolsky) [2]

Both of these articles argue for the thesis that programmers that have been deprived of certain language features often argue that they don't need those features since they are already comfortable working around the lack of said features.

It's a fancy way of arguing: you don't know what you're missing because you've never had it. Or, don't knock it until you try it.

Consider, is your argument a) I've never used it and don't see a need for it, or b) I've used it before and didn't get any benefit?

1. https://paulgraham.com/avg.html?viewfullsite=1

2. https://www.joelonsoftware.com/2006/08/01/can-your-programmi...

show 2 replies
Sharlintoday at 9:33 PM

Discriminated union types are a really fundamental building block of a type system. It's a sad state of matters that many mainstream languages don't have them.

show 2 replies
vips7Ltoday at 9:15 PM

Union/sum types are generally a good thing. Even Java added them. They tend to be worth “the madness”. Now the rest of all the crazy C# features might be a different question.

show 1 reply
munchlertoday at 9:20 PM

Unions are simpler than subclasses and more powerful than enums, so the use cases are plentiful. This should reduce the proliferation of verbose class hierarchies in C#. Algebraic data types (i.e. records and unions) can usually express domain models much more succinctly than traditional OO.

show 1 reply
oompydoompy74today at 9:26 PM

You don’t see the use case for… unions? I’ve got to stop reading the comments. It’s bad for my health.

show 2 replies
andixtoday at 10:03 PM

I've never been confused by language features. Usually the architecture or extreme indirection of the code is the confusing part.

weinzierltoday at 9:24 PM

A common use case for the sum type is to define a Result (or Either) type. Now, C# not having checked exceptions is not as much in need for one as Java is, but I could still imagine it being useful for stream like constructs.

show 1 reply