When would you use a sealed class instead of an enum?
Source: kotlinlang.orgadvanced

This tests closed hierarchies versus singleton constants. A strong answer contrasts enums with sealed class instances, shows a NetworkResult example with data-bearing branches, and highlights exhaustiveness.
This tests Kotlin compile-time closed hierarchies and why sealed classes beat enums for state modeling. A strong answer notes that enum constants are singletons with uniform shape, while sealed subclasses hold distinct payloads like Success(data) and Error(message). It then demonstrates a NetworkResult hierarchy and an exhaustive when expression where the compiler forces every branch to be handled.
Read the original → kotlinlang.org
- #kotlin
- #sealed-classes
- #enums
- #exhaustive-when
- #android
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.