by Fervicus on 8/7/24, 5:02 PM with 302 comments
by tombert on 8/7/24, 5:54 PM
I know not everyone likes them, but for typed languages I find it extremely hard to go back to languages without ADTs of some kind. I do Java for my current job, and Java is generally fine enough, but it's a little annoying when I have to do whacky workarounds with wrapper classes to get something that would be done in three lines of F#.
by kkukshtel on 8/8/24, 3:05 AM
Additionally, excited to now watch this go in and people on HN still act like C# is the same it was 10 years ago.
by tubthumper8 on 8/7/24, 8:31 PM
Is this just a case of "C# developers like to make up different names than established terminology"? (see: SelectMany, IEnumerable, etc.)
by arwhatever on 8/7/24, 5:58 PM
I’ve never felt like I’ve fully understood the implications of the expression problem https://en.wikipedia.org/wiki/Expression_problem but my best/latest personal hypothesis is that providing extension points via conventional polymorphism might be best suited for unknown/future clients who might extend the code, but unions with exhaustive pattern matching seem better suited for code that I or my team owns. I don’t typically want to extend that code. More often, I instead want to update my core data structures to reflect ongoing changes in my understanding of the business domain, more often than not using these Union-type relationships, and then lean on the compiler errors maximally to get feedback about where the rest of the imperative code now no longer matches the business domain data structures.
by orra on 8/7/24, 6:07 PM
But this looks like a discriminated union which I'd recognise from F# or Haskell. The distinction I'd draw is that for a DU there are named case constructors. Yes?
by arnath on 8/8/24, 6:07 AM
I’m pretty sure you can implement the example in this proposal by declaring an empty interface and having some record classes that “implement” it. Does that lose something that I’m not seeing?
by LandR on 8/7/24, 6:09 PM
> Note: Have Mads write this part.
:)
by zamalek on 8/7/24, 5:52 PM
Having _attempted_ (and being bitten by) far too much black magic with C# unions in the past (using FieldOffset), there is an unfortunate situation here: aliasing a pointer/ref value and value is UB. This means that a struct union of a u64 and an object would need separate fields for each, wasting 8 bytes. That is, unless the ryujit/gc is updated with knowledge about this.
by LAC-Tech on 8/7/24, 8:42 PM
I know I know, pseudo OO took over the world, and then people revolted against it, so the easiest thing to do to stay relevant is to become "slightly functional with curly braces", rather than to actually try and do OO well.
by ctenb on 8/7/24, 6:35 PM
by nsonha on 8/8/24, 8:35 AM
by gregsn on 8/10/24, 6:53 PM
Siamese pet = ...;
(Cat or Chihuahua) mostlyCats = pet;
Dog dog = (Dog)mostlyCats;
Note: This works for implemented interfaces too.I am unsure what that section entails...
Let's say I have a function like this:
void ConsumeAB<T>(T ab) where T: IA, IB;
And implementing classes like this: class ABC : IA, IB {}
class ABD : IA, IB {}
Will I be able to use this function like this: var items = new (ABC or ABD)[] { new ABC(), new ABD() };
foreach (var item in items)
{
ConsumeAB(item);
}
In other words will the ad hoc union act as it would implement all interfaces that all cases have in common? My guess it won't work, as it gets lowered to code that uses object. Would a custom union help me here?by mlhpdx on 8/7/24, 11:38 PM
Edit: typo
by facturi on 8/8/24, 1:36 AM
Tearing can cause memory safety issue. Variant A can have an integer field and variant B can have a reference field in the same offset. Tearing can cause it to treat an integer as reference.
by jtrowbridge on 8/7/24, 11:24 PM
by wackro on 8/7/24, 6:12 PM
I was considering introducing the OneOf library into our codebase, but if this is < a year or so away it might not be worth the effort.
by Alifatisk on 8/7/24, 7:51 PM
by rr808 on 8/8/24, 10:25 AM
by yas_hmaheshwari on 8/8/24, 4:24 AM
by ComputerGuru on 8/7/24, 6:00 PM
But for the projects that fall right about in the middle where it could go either way and I could see either language working, I almost always pick rust with discriminated unions being the biggest reason. It's incredibly hard to go back to a language without even basic ADTs after seeing the light.
by riizade on 8/7/24, 10:51 PM
If this feature were implemented, it would take C# to a top contender for a daily driver language for me, and would make me feel a lot more confident in choosing it for projects where the C# ecosystem is already present, such Godot's C# version.
I'm really excited about this proposal, and I don't know anything about C#'s development pace, but I'll be watching from the sidelines hoping I can use union types in production in 2025 or so.
by ctenb on 8/7/24, 6:31 PM
by pipeline_peak on 8/7/24, 9:20 PM
Seems more confusing than useful. Wish they’d stop adding onto this ever expanding language.
It’s like these people get bored of plain old functions and objects, read an academic paper and go “i think I’ll add that next”.