The login example makes the benefit of handling every outcome concrete. Iβd also give the lockout logic a clock-based test: fail three times, advance past the intended unlock time, then attempt a valid login. That checks the behaviour behind AccountLocked, separately from whether every result type has a matching branch.
Thatβs fair. I meant it as a follow-up exercise rather than something the introduction needed to cover. The login example already makes the union-handling point easy to follow.
Setting CS8509 as an error is the part Iβll actually use. I tested nullable on an old project this week, and the lesson was the same: a warning nobody is forced to fix usually doesnβt get fixed.
The default struct hole is a good catch too. The compiler narrows the gaps, it doesnβt close all of them.
Union is like a T-shirt that is one fit for all specified sizes.Like brothers share a T-shirt only with each other not any outside person. And only one can wear it at a time! π
I research AI tools, APIs & agents β comparing specs, pricing and real user reports β then publish honest rankings so you don't waste money. Research-first, never sponsored fluff.
the bit about var and _ matching the union itself caught me off guard β I'd have assumed a switch arm always unwraps, so that's exactly the thing I'd get wrong on first use. the login example sells it though, a new case turning into a warning instead of a runtime crash is the whole point
This is exciting! One thing I find particularly interesting is how unpacked unions could make state handling more predictable by moving certain mistakes from runtime to compile time.
It reminds me of Flutter's sealed classes, where explicitly modeling loading, success, and error states makes applications easier to maintain. The real win isn't just writing less code; it's making invalid states harder to represent in the first place.
Now I'm curious: could this change how C# developers approach API responses and complex state management?
One gap I'd watch when adopting this: the exhaustiveness guarantee lives on switch expressions. Rewrite the same dispatch as a switch statement or an if/else chain and the compiler stays quiet when a fourth case type appears later, the code just falls through. I reproduced it on the .NET 11 preview SDK after reading your repo, and two other writeups on C# 15 unions note the same limitation.
Is an analyzer for the statement form on the roadmap, or is the expectation that style rules push everyone to expressions? In a codebase I maintain we lint for expression-only dispatch over closed hierarchies, and it has caught silent fall-throughs twice. Your login example ported cleanly, by the way.
Having compiler-checked exhaustive matching on a closed list of types instead of maintaining a throwaway default arm for 'impossible' subclasses is huge. Comparing the before-and-after login flow makes the boilerplate drop obvious right away.
A most commendable advancement for the C# fraternity! The true merit of union types, methinks, lies not merely in tidier code, but in sparing developers from the peril of overlooked possibilities. Having the compiler itself call attention to unhandled cases is a considerable boon, particularly in projects that grow and evolve over time. A fine exposition, George; I should be most interested to witness how this feature fares in larger, more intricate applications.
The login example makes the case well: adding TwoFactorRequired and getting a compiler warning at every switch is the real payoff, not just shorter code. One decision rule I would like to hear more about is when you would still choose a closed hierarchy over a union. Unions cannot add instance fields, so shared behavior has to live elsewhere, and a sealed abstract record can carry it. Is that the main boundary for you, or is there another case where the older pattern still wins?
Union types pay off when the exhaustiveness story reaches the boundary where errors and domain alternatives are handled. The value is not just fewer nulls; it is making an unhandled case difficult to compile and easy to locate during review.\n\nExamples that contrast a nullable or exception-heavy API with a closed result union would make the practical benefit especially clear for teams deciding where to introduce them first.
The login example makes the benefit of handling every outcome concrete. Iβd also give the lockout logic a clock-based test: fail three times, advance past the intended unlock time, then attempt a valid login. That checks the behaviour behind AccountLocked, separately from whether every result type has a matching branch.
Sure. But I didnβt want to complicate the example too much.
Thanks for the feedback.
Thatβs fair. I meant it as a follow-up exercise rather than something the introduction needed to cover. The login example already makes the union-handling point easy to follow.
Setting
CS8509as an error is the part Iβll actually use. I tested nullable on an old project this week, and the lesson was the same: a warning nobody is forced to fix usually doesnβt get fixed.The default struct hole is a good catch too. The compiler narrows the gaps, it doesnβt close all of them.
Thatβs a useful setting. Itβll make sure you donβt accidentally skip any of the possible options.
Union is like a T-shirt that is one fit for all specified sizes.Like brothers share a T-shirt only with each other not any outside person. And only one can wear it at a time! π
That is a brilliant analogyπ
the bit about var and _ matching the union itself caught me off guard β I'd have assumed a switch arm always unwraps, so that's exactly the thing I'd get wrong on first use. the login example sells it though, a new case turning into a warning instead of a runtime crash is the whole point
Thanks!
This is exciting! One thing I find particularly interesting is how unpacked unions could make state handling more predictable by moving certain mistakes from runtime to compile time.
It reminds me of Flutter's sealed classes, where explicitly modeling loading, success, and error states makes applications easier to maintain. The real win isn't just writing less code; it's making invalid states harder to represent in the first place.
Now I'm curious: could this change how C# developers approach API responses and complex state management?
Thanks!
I think not immediately, not to break existing code, but after some time we might see some changes in how all that is handled.
One gap I'd watch when adopting this: the exhaustiveness guarantee lives on switch expressions. Rewrite the same dispatch as a switch statement or an if/else chain and the compiler stays quiet when a fourth case type appears later, the code just falls through. I reproduced it on the .NET 11 preview SDK after reading your repo, and two other writeups on C# 15 unions note the same limitation.
Is an analyzer for the statement form on the roadmap, or is the expectation that style rules push everyone to expressions? In a codebase I maintain we lint for expression-only dispatch over closed hierarchies, and it has caught silent fall-throughs twice. Your login example ported cleanly, by the way.
Hm... this is interesting, to be honest, I haven't thought about that yet. Thank you!
Having compiler-checked exhaustive matching on a closed list of types instead of maintaining a throwaway default arm for 'impossible' subclasses is huge. Comparing the before-and-after login flow makes the boilerplate drop obvious right away.
Agreed. The new approach is so much better.
A most commendable advancement for the C# fraternity! The true merit of union types, methinks, lies not merely in tidier code, but in sparing developers from the peril of overlooked possibilities. Having the compiler itself call attention to unhandled cases is a considerable boon, particularly in projects that grow and evolve over time. A fine exposition, George; I should be most interested to witness how this feature fares in larger, more intricate applications.
Thank you!
I guess we're gonna find out when we start using it for real! And I wonder how .NET 12 will take this further.
The login example makes the case well: adding TwoFactorRequired and getting a compiler warning at every switch is the real payoff, not just shorter code. One decision rule I would like to hear more about is when you would still choose a closed hierarchy over a union. Unions cannot add instance fields, so shared behavior has to live elsewhere, and a sealed abstract record can carry it. Is that the main boundary for you, or is there another case where the older pattern still wins?
The new pattern mostly wins.
Union types pay off when the exhaustiveness story reaches the boundary where errors and domain alternatives are handled. The value is not just fewer nulls; it is making an unhandled case difficult to compile and easy to locate during review.\n\nExamples that contrast a nullable or exception-heavy API with a closed result union would make the practical benefit especially clear for teams deciding where to introduce them first.
You're correct, but that "\n\n" looks kinda suspicious π