Article

Modeling aggregates with sealed classes

Jan 30, 2025 · 10 min

An Aggregate is a consistency boundary. Kotlin’s sealed hierarchies let you encode its invariants so invalid states simply won’t compile.

sealed interface Cart {
  data object Empty : Cart
  data class Active(val lines: List<Line>) : Cart
}