Skip to content
Commitwise
Menu

Sealed hierarchy

Also called: Sealed class, Sealed interface, Sum type, Tagged union

A type whose list of subtypes is fixed and known to the compiler, so a switch over it can be checked for completeness.

Definition

A sealed class or interface only allows the subtypes declared next to it (in the same module and package in Kotlin, listed in permits in Java). Because the compiler knows every subtype, a when or switch that handles all of them needs no default branch, and adding a subtype makes every incomplete switch fail to compile. Swift gets the same effect with enums that have associated values.

In practice

sealed interface SearchState with Idle, Loading, Loaded(products) and Failed(message): a screen is always in exactly one of them.

Learn it in context

Lessons that use this term.

Related terms

Esc

Type to search the whole site.