Live data from Hacker News

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

andrewlock.net

141–150 of 300 posts

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

#141
post #139

Finally C# will have a more idiomatic way to represent this classic code example from “The Daily WTF” in 2005 — enum Bool { True, False, FileNotFound }; See https://thedailywtf.com/articles/what_is_truth_0x3f_

Could False = FileNotFound and this is a case of AST symbol constraints?

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

#142
post #92
post #5

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

Well it is a type union. The union of string and string is just string.

No, it's a union of a left value (that happens to be a string) and a right value (that happens to be a string). But the compiler-generated code can't tell them apart.

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

#143
post #100

Earlier quoted context omitted.

I guess C# is more strongly-typed than Haskell then... /s

You cant have a `type Foo = String | Strimg` in Haskell either.

But you can have an `Either String String` which is what GP was talking about.

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

#144
post #135

Earlier quoted context omitted.

> That's like saying Haskell is really an imperative language just because it's written in C I honestly don't even remotely understand how you got to that from what I wrote. Unions already have a definition. You cannot go around claiming that technically unions are not unions (and that "most people don't know" this), because theory X or Y overloads the term union to refer to some unrelated concept. That's fine and we…

> You cannot go around claiming that 'technically' unions are not unions I never said this. I said that there are two different kinds of unions with quite different definitions and behaviors. > A tagged union is so called because it is a union (overlapping memory) with a field (a tag) containing a discriminator (aka discriminated union). An untagged union is a union without this tag. That alone doesn't sufficiently d…

[deleted]

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

#145
post #5

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

You can use implicit operators or a library like Vogen to accomplish the same thing in a way that they can be coerced as strings. This isn’t a real issue.

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

#146
post #135

Earlier quoted context omitted.

> That's like saying Haskell is really an imperative language just because it's written in C I honestly don't even remotely understand how you got to that from what I wrote. Unions already have a definition. You cannot go around claiming that technically unions are not unions (and that "most people don't know" this), because theory X or Y overloads the term union to refer to some unrelated concept. That's fine and we…

> You cannot go around claiming that 'technically' unions are not unions I never said this. I said that there are two different kinds of unions with quite different definitions and behaviors. > A tagged union is so called because it is a union (overlapping memory) with a field (a tag) containing a discriminator (aka discriminated union). An untagged union is a union without this tag. That alone doesn't sufficiently d…

> Maybe they are implemented similarly under the hood

It's hard to know what to say to this. Tagged unions are not tagged unions, even if they are literal tagged unions? What?

I reiterate what I said in my previous comment, you're not using the ordinary definition of the term union, and this is causing confusion. A union may or may not be a "union" as understood within various academic type theories, that really depends on how any given theory defines that word, which can be any way it wants. But a union is a CS concept with a clearly understood meaning, and when used without added context to suggest it is to be interpreted in some theoretical way, it is understood in that ordinary way.

OP's article is clearly using 'union' to mean tagged unions - he even shows off their implementation, with a tag. The author assumes that his audience will understand what he's talking about when he uses the word union, and it's not causing anyone trouble in this comments section. The fact that alternative definitions within various theoretical paradigms is very nice, bless their hearts, but not really relevant.

You may prefer other definitions to the usual CS definition, that's certainly your prerogative, but - again - that's hardly grounds for taking an article and comment section that's using the commonplace meaning, and appearing to lecture others for failing to adhere to your idiosyncratic standards for what a union must be.

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

#147

Earlier quoted context omitted.

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.

yeah this is the one I've considered as being mildly compelling. But don't we lose the fun of having exception handling as separate to the happy path?

oo and support for exceptions, in particular checked exceptions, was a mistake of the 90s. We know better today, there’s a reason for why modern languages like go/rust/swift don’t use them, and why many use c++ with exceptions disabled.

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

#148
post #108
post #31

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.

Do you think by now C# has left Java behind in features and performance?

It always has.

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

#149
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 it's almost always about making code more concise and programming more ergonomic. Assembly could already solve all the problems higher-level languages can solve. Yet we didn't discard them as useless.

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

#150
post #146

Earlier quoted context omitted.

> You cannot go around claiming that 'technically' unions are not unions I never said this. I said that there are two different kinds of unions with quite different definitions and behaviors. > A tagged union is so called because it is a union (overlapping memory) with a field (a tag) containing a discriminator (aka discriminated union). An untagged union is a union without this tag. That alone doesn't sufficiently d…

> Maybe they are implemented similarly under the hood It's hard to know what to say to this. Tagged unions are not tagged unions, even if they are literal tagged unions? What? I reiterate what I said in my previous comment, you're not using the ordinary definition of the term union, and this is causing confusion. A union may or may not be a "union" as understood within various academic type theories, that really depe…

See my edit above. There is the C terminology, and the TypeScript et al terminology, and it's the same "untagged" terminology for two different things. In any case, both these kinds of "untagged" unions are different from tagged/discriminated unions. So just calling them "unions" is ambiguous at best and confusing at worst.
Post reply on HN