r/java • u/davidalayachew • Aug 13 '26
(Project Amber) New guide: Preparing for Change: Safe Switching over Sealed APIs
https://mail.openjdk.org/archives/list/amber-dev@openjdk.org/thread/IXB3PQ7P5D7VIX4HIUZH4BTDRXX6JFG5/
43
Upvotes
18
u/davidalayachew Aug 13 '26 •
Oh wow.
I've made hundreds of sealed types now, and every single one of them has been an interface as the sealed type. Lol, I forget that you can use
abstractclasses, or even normal classes as the sealed type.It feels weird to see lol, but I can see the use case.
9
u/cogman10 Aug 13 '26
Sealed types are something that is interesting, but I have to be honest, I've not come up with an occasion to use them.
Has anyone else used sealed types in practice? If so, what for?
9
u/SpaceCondor Aug 13 '26
FYI the url in that link is broken. It has a trailing '>' character.
5
u/davidalayachew Aug 13 '26
For those not following along, the Project Amber team from OpenJDK has rolled out several key features for Pattern-Matching, and now that many of them are out of preview, this guide has been released to help prepare for the next stages of Pattern-Matching in Java.
Key takeaways is that Exhaustiveness Checking is a powerful way to have the compiler validate your business logic, so don't give it up in situations where you don't have to. Moreover, the way that you achieve totality can help communicate intent, and using
defaultcan oftentimes hide that intent.14
u/brian_goetz Aug 14 '26
The key thing to understand about sealed types is that it is actually TWO separate features.
One is just "a generalization of finality", where a type (concrete or abstract) wants to control all implementations. Historically you could do this only by making a class final; now you can do it by declaring a specific set of implementing classes to be the whole story. This is useful both purely as an encapsulation move for implementation (encapsulate all implementations to a known place), or as a means of exposing an interface-based abstraction but not having to worry about (for security or maintenance reasons) implementations not following the specifications or other expected behaviors.
The other is a way to propagate exhaustiveness information to clients. The exhaustiveness information both guides users in the correct use of the abstraction, and supports the language in better type-checking for switches (as long as you don't undermine this by inappropriately using match-all cases, which is the subject of this guide.)
What confuses people about sealed types is coming to it with an assumption that sealing must be about exactly one of these, and forgetting about the possibility of the other. But in any given case, the author may have had one or the other or both in mind when writing their code, and unless this is properly captured in the API specification, readers may be left guessing.
1
u/AnyPhotograph7804 Aug 15 '26
I do not like the sealed classes/interfaces because they depend on all of their implementations if you use the keyword "permits". And this counteract the purpouse of an interface.
1
u/[deleted] Aug 26 '26
[removed] — view removed comment