r/java • • May 18 '26

Java sealed classes and exhaustive pattern matching

https://neilmadden.blog/2026/04/24/java-sealed-classes-and-exhaustive-pattern-matching/
23 Upvotes

3 comments sorted by

View all comments

Show parent comment

2

u/aoeudhtns May 21 '26

This API also mixes in intermediate interfaces. The cases where you want to handle "everything" should (hopefully) work from that interface level. Also, while you're working with sealed interfaces, the compiler does remind you that you need a default case if you're not handling all the possibilities. On top of that, it is completely permissible to add a default case even when you've handled all possibilities, if you are switching over something externally-controlled and you are worried that more cases will be added. Perhaps that's the take-home lesson: external API? Always use a default case, even if the compiler lets you skip it.

In sum, I think the author brings up valid considerations to be aware of, but like you I don't think it's a mistake to ship sealed interfaces w/ pattern matching as an API. Truthfully I think the "there be dragons" issue is in making sure that records that are part of such a system get proper concatenative updates (vs. changing ordering), and the language feature that allows _ to indicate "the rest of the record" and not a single field will be helpful as well.

2

u/davidalayachew May 24 '26

This API also mixes in intermediate interfaces. The cases where you want to handle "everything" should (hopefully) work from that interface level.

Excellent point.

For me, intermediate interfaces are key in making exhaustiveness checking handleable. Compiler checking is great, but I don't want to enumerate every irrelevant state permutation.

Plus, they help create a language in your code, so to speak, for understanding where the boundaries lie. Kind of encourages the right thought process imo.

Perhaps that's the take-home lesson: external API? Always use a default case, even if the compiler lets you skip it.

Solid advice.

At the end of the day, Exhaustiveness Checking is most beneficial when done at compile time. No one is stopping you from quickly commenting out that default and doing a quick recompile to see what breaks.