Discussion
Explore union types in C# 15
98347598: It's very disappointing that they aren't supporting Rust-style discriminated unions.
karmakaze: I haven't read this in detail but I expect it to be the same kind of sealed type that many other languages have. It doesn't cover ad-hoc unions (on the fly from existing types) that are possible in F# (and not many non-FP languages with TypeScript being the most notable that does).
orthoxerox: Third paragraph from the top:> unions enable designs that traditional hierarchies can’t express, composing any combination of existing types into a single, compiler-verified contract.
SideburnsOfDoom: It's very unclear which you mean by that.To me that "compiler-verified" maps to "sealed", not "on the fly".Their example is:public union Pet(Cat, Dog, Bird);Pet pet = new Cat("Whiskers");- the union type is declared upfront, as is usually the case in c#. And the types that it contains are a fixed set in that declaration. Meaning "sealed" ?
FrustratedMonky: Is this the last of the F# features to be migrated into C#?What a missed opportunity. I think really F# if you combine all of its features, and what it left out, was the way. Pulling them all into C# just makes C# seem like a big bag of stuff, with no direction.F#'s features, and also what it did not included, gave it a style and 'terseness', that still can't really be done in C#.I don't really get it. Was a functional approach really so 'difficult'? That it didn't continue to grow and takeover.
owlstuffing: > Pulling them all into C# just makes C# seem like a big bag of stuff, with no direction.Agreed. Java is on the same trail.
pjc50: OK then, what is the opposite of this, the adhoc union?
SideburnsOfDoom: I don't follow the question. Maybe define the term that you are using?
pjc50: Top comment mentioned the term without defining it, confusing me and seemingly most of the thread: https://news.ycombinator.com/item?id=47649817
owlstuffing: > It doesn't cover ad-hoc unionsYes and no. C# unions aren’t sealed types, that’s a separate feature. But they are strictly nominal - they must be formally declared: union Foo(Bar, Baz); Which isn’t at all the same as saying: Bar | Baz It is the same as the light and day difference between tuples and nominal records.
CharlieDigital: > I don't really get it To me it makes sense because C# is a very general purpose language that has many audiences. Desktop GUI apps, web APIs, a scripting engine for gaming SDKs, console apps.It does each reasonably well (with web APIs being where I think they truly shine). > Was a functional approach really so 'difficult' It is surprisingly difficult for folks to grasp functional techniques and even writing code that uses `Func`, `Action`, and delegates. Devs have no problem consuming such code, but writing such code is a different matter altogether; there is just very little training for devs to think functionally.
gib444: Is C# a great language trapped in a terrible ecosystem? ie would masses use C# if it existed in another ecosystem?Or is it becoming a ball-of-mud/bad language compared to its contemporaries?(Honest questions. I have never used .NET much. I'm curious)
JCTheDenthog: Depends on what you mean by ecosystem, it hasn't been trapped on Windows for about a decade now. The variety of third party libraries available is quite good, while the standard library is robust enough that you don't need NPM nonsense like LeftPad and IsEven and IsNumber.Are there particular things about the ecosystem that you worry about (or have heard about)? Biggest complaint I would have is that it seems like many popular open source libraries in the .NET ecosystem decide to go closed source and commercial once they get popular enough.
raincole: Union is almost a net positive to C# in my opinion.But I do agree. C# is heading to a weird place. At first glance C# looks like a very explicit language, but then you have all the hidden magical tricks: you can't even tell if a (x) => x will be a Func or Expression[0], or if a $"{x}"[1] will actually be evaluated, without looking at the callee's signature.[0]: https://learn.microsoft.com/en-us/dotnet/csharp/advanced-top...[1]: https://learn.microsoft.com/en-us/dotnet/csharp/advanced-top...
CharlieDigital: C# is a language that serves many masters and if you trace the origin of its featureset, you can see why each was created. Take the `dynamic` keyword: created to support interfacing with COM interop easier[0].It serves many audiences so it can feel like the language is a jack of all trades and master of none (because it is) and because it is largely backwards compatible over its 20+ years of existence.That said, I think people make a mountain out of a molehill with respect to keyword sprawl. Depending on what you're building, you really only need to focus on the slice of the language and platform you're working with. If you don't want to use certain language features...just don't use them?I think it excels in a few areas: web APIs and EF Core being possibly the best ORM out there. For me, it is "just right". Excellent platform tooling, very stable platform, very good performance, hot reload (good, but not perfect), easy to pick up the language if you already know TypeScript[1]; there are many reasons it is a good language and platform.[0] https://learn.microsoft.com/en-us/dotnet/csharp/advanced-top...[1] https://typescript-is-like-csharp.chrlschn.dev/
pjmlp: Microsoft's management has always behaved as if it was a mistake to have added F# into Visual Studio 2010, and being stuck finding a purpose for it.Note that most of its development is still by the open source community and its tooling is an outsider for Visual Studio, where everything else is shared between Visual Basic and C#.With the official deprecation of VB, and C++/CLI, even though the community keeps going with F#, CLR has changed meaning to C# Language Runtime, for all practical purposes.Also UWP never officially supported F#, although you could get it running with some hacks.Similarly with ongoing Native AOT, there are some F# features that break under AOT and might never be rewritten.A lost opportunity indeed.
gib444: Yup, the commercial libraries. That's pretty big. It's nice the standard library has lots of goodies, but I doubt many projects in reality are zero-dependency(The amount of times I hear "the standard lib is great!" seems more to attempt to defend the plethora of commercial libraries, more than anything)The community feels rather insular too? The 9-5 dayjob types with employers who don't understand or embrace open source? At my age I can respect that thoughAnd is Postgresql a 2nd-class citizen? If so, your boss will tell you to use SQL Server surely?I guess it's hard to get a grasp on the state/health of .NET as to me it seems 99.99999% of the code is in private repos companies, as it's not a popular choice for open source projects. Which itself seems like a proxy signal though