Android ● ● ○ Kotlin 2.0

Kodebits Day 57: When With Sealed Class

Jul 13 2026
Practice when with a short kotlin challenge.

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]


Further Reading