Enums and sealed result types
Enums and sealed result types
A mark can be pending or recorded, and only a recorded state should carry a score. Enums describe fixed named alternatives, while a sealed hierarchy can model alternatives with different data. Keeping the states explicit avoids unrelated flags that can disagree.
What you’ll learn
- Model typed alternative result states.
- Handle sealed alternatives exhaustively.
Enums and sealed result types in the example
Known alternatives
A mark can be pending or recorded, and only a recorded state should carry a score.
Known alternativesMatch actual state
A sealed interface restricts its direct implementers under Kotlin’s rules and supports exhaustive when handling of known alternatives.
Match actual stateRead case-specific data
Pending needs no fabricated numeric score.
Read case-specific data
Understand enums and sealed result types
A sealed interface restricts its direct implementers under Kotlin’s rules and supports exhaustive when handling of known alternatives. A data object names a singleton state; a data class can carry case-specific values. An enum is useful when each constant fits the same declared type structure.
Connect this to when expressions and matching and smart casts and early validation.
Follow enums and sealed result types
Save the complete source as Main.kt and use the Kotlin/JVM environment. No separate input file is needed.
Kotlin/JVM · complete program
sealed interface Result
data object Pending : Result
data class Recorded(val score: Int) : Result
fun describe(result: Result): String = when (result) {
Pending -> "Awaiting result"
is Recorded -> "Score: ${result.score}"
}
fun main() {
println(describe(Pending))
println(describe(Recorded(80)))
}Expected output
Awaiting result
Score: 80Follow the execution
Pending needs no fabricated numeric score. The Recorded branch narrows the type and reads its score. Both alternatives are handled, so the function returns a message for each possible demonstrated state.
Try this in the Kotlin compiler by pasting the complete program.
Practice: Enums and sealed result types
Your task: What should you do after adding another permitted result state?
Kotlin/JVM · complete program
sealed interface Result
data object Pending : Result
data class Recorded(val score: Int) : Result
fun describe(result: Result): String = when (result) {
Pending -> "Awaiting result"
is Recorded -> "Score: ${result.score}"
}
fun main() {
println(describe(Pending))
println(describe(Recorded(80)))
}Worked answer
Update the exhaustive handling where necessary so the new state has an intentional meaning instead of being silently ignored.
Enums and sealed result types: the important boundary
Use enum entries for simple fixed labels, and sealed alternatives for richer state models. Type structure still does not validate a Recorded score’s range unless its construction or operations enforce that policy.
Check your understanding
Enums and sealed result types: what to keep in mind
Update the exhaustive handling where necessary so the new state has an intentional meaning instead of being silently ignored. Use enum entries for simple fixed labels, and sealed alternatives for richer state models.
Further reading: official Kotlin documentation for this topic.
Reaching the recap and completing the quizzes are separate milestones.