News for hackers. Stuff that matters.
Re:Java sealed classes and exhaustive pattern matching by rf15 on Monday October 19, 2026 @04:11AM (#50149693)
I disagree - the problem lies with the intention of exhaustively listing all available values in a switch expression with no backup plan. Switch expressions should always have a default/fallback branch, even if it's theoretically impossible and only logs the attempt/picks a default for future proofing the code and its uses.
Re:Java sealed classes and exhaustive pattern matching by misja111 on Monday October 19, 2026 @04:32AM (#50149831)
> Switch expressions should always have a default/fallback branch,

This will nullify the advantage of being able to treat sealed classes as algebraic datatypes by using the exhaustiveness check.

Re:Java sealed classes and exhaustive pattern matching by rf15 on Monday October 19, 2026 @05:50AM (#50150379)
Yes and no. You either accept that your code and the environment is final (in which case there's no problem and you can just blindly use the exhaustiveness check) or you accept that your code is a living thing that can change in part (including your edits, its dependencies, and its standard library), in which case you must be prepared for changes but can still make use of the exhaustiveness check by temporarily removing the default case.

I feel this is fundamentally a perspective issue. If this is a problem, also consider why people want sealed classes in the first place. It's always a tradeoff and you can also always fork or propose your own improved version of the concept and see if it has enough merit to gain traction. There's a million other footguns in the language that needs similar scrutiny, I'm just happy java isn't C/C++.

Re:Java sealed classes and exhaustive pattern matching by carlmr on Monday October 19, 2026 @06:05AM (#50150472)
We could have both by introducing another keyword than "default" here.

In some cases you want "default" handling, in other cases it's a runtime detection of a code issue. E.g. you could add a "fallback" case which is the same as "default" yet if your code doesn't exhaustively handle all sealed class cases won't compile. This would still create a fallback, if your sealed class is widened later on, but still allow you to error out if it's already the case right now.

Re:Java sealed classes and exhaustive pattern matching by masklinn on Monday October 19, 2026 @06:59AM (#50150846)
> You either accept

That is a false dichotomy. Some enumerations have reasons to likely change in the future, others (most) either have no such thing or their changing would be so impactful to the system it needs to be signaled loudly.

That’s really no different than any other type information.

> It's always a tradeoff and you can also always fork or propose your own improved version of the concept

You are the person asserting that the concept as defined is incorrect.

Re:Java sealed classes and exhaustive pattern matching by rf15 on Monday October 19, 2026 @09:24AM (#50152167)
No, I assert that the concept as defined has a reasoning behind it that accepts certain tradeoffs for certain benefits. I think its perfectly valid to disagree with them, but I am curious about improvements to the current solution to the problem of extending types that really should not get extended (e.g. from the standard lib)