Live data from Hacker News

.NET (OK, C#) finally gets union types

andrewlock.net

41–50 of 300 posts

Re: .NET (OK, C#) finally gets union types

#41
post #23

Earlier quoted context omitted.

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.

ok, so what problems do they help me solve that I can't already solve? Is it just that we can make code more concise or am I missing a trick somewhere?

I think "what problems do they solve that I can't already solve" is the wrong way to look at it. After all, ultimately most language features are just syntactic sugar - you could implement for loops with goto, but it would be a lot less pleasant. I think that unions aren't strictly necessary, but they are a very pleasant to use way of differentiating between different, but related, types of value.

Re: .NET (OK, C#) finally gets union types

#42

Earlier quoted context omitted.

ok, so what problems do they help me solve that I can't already solve? Is it just that we can make code more concise or am I missing a trick somewhere?

I think "what problems do they solve that I can't already solve" is the wrong way to look at it. After all, ultimately most language features are just syntactic sugar - you could implement for loops with goto, but it would be a lot less pleasant. I think that unions aren't strictly necessary, but they are a very pleasant to use way of differentiating between different, but related, types of value.

Ok. I'm just trying to understand what code I'm replacing with them. Like I wanna see the before and after in order to gain the same level of excitment as other people seem to have for them.

Often the explanations just seem rather abstract which makes it harder to appreciate the win, versus the hideous sort of code that might appear when they're misused.

Re: .NET (OK, C#) finally gets union types

#43

Earlier quoted context omitted.

[flagged]

I love discriminated unions. The problem with C# is that it’s so overloaded with features. If you come from one codebase to another codebase by a different team it’s close to learning a completely new language, but worse, there is no documentation I can find that will teach me only about that language. Throw in all the versioning issues and the fact that .Net shops aren’t great about updating to the latest versions,…

I am all for minimalism but "If you come from one codebase to another codebase by a different team it’s close to learning a completely new language" I really don't agree. It's not that big. Just sounds like a skill issue

Re: .NET (OK, C#) finally gets union types

#44
post #9

Earlier quoted context omitted.

What features do you see as crazy?

All the weird cruft around nullability, for starters. Once again confirming that allowing null references is usually a mistake.

Do you mean the implicit nullable types? Now that you can make nullable explicit instead I really don’t have much issues with it. It is part of the type system, as it should, and you have null coalescing operators. Is it still problematic or are you dealing with older codebases where you cannot set the nullable pragma?

Re: .NET (OK, C#) finally gets union types

#45

Earlier quoted context omitted.

[flagged]

I love discriminated unions. The problem with C# is that it’s so overloaded with features. If you come from one codebase to another codebase by a different team it’s close to learning a completely new language, but worse, there is no documentation I can find that will teach me only about that language. Throw in all the versioning issues and the fact that .Net shops aren’t great about updating to the latest versions,…

none of that applies to my position. I have an appreciation for almost all of C# and am comfortable in the framework. I just want to know what situations would be better suited to using them than traditional approaches.

I get there's an .Either pattern when chaining function calls so you don't have to do weird typing to return errors, but I'm using exceptions for that anyway, so the return type isn't an issue.

Re: .NET (OK, C#) finally gets union types

#46
post #2

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

Haskell, OCaml, Erlang lead the way and Rust, Zig and Go get all the mindshare. I feel like its a common pattern for more experimental languages to pioneer features and other languages to copy the features and bring them to a C style syntax that the majority of devs are familiar with.

I wish I could find the reference, but there was a great blog / article by a computer science academic basically saying that OO, procedural, and functional paradigms are extremes of a design space where the “middle” of its Pareto frontier was essentially unknown until recent advances.

Moreover, many functional languages are getting pseudo-procedural features via the like of “do” syntax and monads, but that this is in some sense a double abstraction over the underlying machine that is already inherently procedural.

Starting from a language that is already procedural and sprinkling some functional abstractions on top is simpler to implement and easier for humans to use and understand.

Rust especially showed that many of the supposed advantages of functional languages are not their exclusive domain, such as sum types and a powerful type system.

Update: Hah! ChatGPT found it: https://news.ycombinator.com/item?id=21280429

Note the top comment especially, which explains succinctly why functional has rather substantial downsides.

Re: .NET (OK, C#) finally gets union types

#47

Earlier quoted context omitted.

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.

> so the use cases are plentiful such as? > This should reduce the proliferation of verbose class hierarchies in C# So just as an alternative for class hierarchies? I mean good people already balance that by having a preference for composition.

“Compoision”. A typo I know but it would be a word describing what goes wrong with class hierarchies.

Re: .NET (OK, C#) finally gets union types

#48
post #23

Earlier quoted context omitted.

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.

ok, so what problems do they help me solve that I can't already solve? Is it just that we can make code more concise or am I missing a trick somewhere?

Simple example that I use often when writing API clients:

In current C# I usually do something like

public class ApiResponse { public T? Response { get; set; } public bool IsSuccessful { get; set; } public ErrorResponse Error { get; set; } }

This means I have to check that IsSuccessful is true (and/or that Response is not null). But more importantly, it means my imbecile coworkers who never read my documentation need to do so as well otherwise they're going to have a null reference exception in prod because they never actually test their garbage before pushing it to prod. And I get pulled into a 4 hour meeting to debug and solve the issue as a result.

With union types, I can return a union of the types T and ErrorResponse and save myself massive headaches.

Re: .NET (OK, C#) finally gets union types

#49
post #5

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

but can you define T1 and T2 of string, then use Either ?

Could you be clearer about what you mean, since string is a sealed type in C#, so what exactly do you mean T1 and T2 of string?

Re: .NET (OK, C#) finally gets union types

#50
post #9
post #7

Earlier quoted context omitted.

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.

What features do you see as crazy?

Maybe not crazy but the language just has a really broad surface. I find it to be like the Scala of the OO world.
Post reply on HN