What does this print?
sealed class Res
data class Ok(val n: Int) : Res()
data object Err : Res()
fun tag(r: Res) = when (r) {
is Ok -> r.n * 2
Err -> 0
}
fun main() {
println(tag(Ok(5)))
}
Try it in the online Kotlin Playground →
[spoiler title="Solution"]
Answer:
10
Explanation:
Sealed classes restrict inheritance to known subclasses, allowing when expressions to verify exhaustive handling at compile time.
[/spoiler]