Exhaustive when
Cover all cases.
Exhaustive when is a free Kotlin Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Covering All Cases
An exhaustive when handles every possible value. When used as an expression, Kotlin requires this so a result is always produced.
This lesson shows how to make when exhaustive and why it helps.
else Makes It Exhaustive
For open-ended subjects like Int, an else branch covers everything not listed.
This guarantees the when always matches.
fun label(n: Int): String = when (n) {
0 -> "zero"
1 -> "one"
else -> "many"
}
fun main() {
println(label(0))
println(label(5))
}Enums Enable Exhaustiveness
With an enum, the compiler knows every possible value. List them all and you may omit else.
enum class Direction { NORTH, SOUTH, EAST, WEST }
fun turn(d: Direction): String = when (d) {
Direction.NORTH -> "up"
Direction.SOUTH -> "down"
Direction.EAST -> "right"
Direction.WEST -> "left"
}
fun main() {
println(turn(Direction.EAST))
}Missing an Enum Case
If you forget an enum value in an expression when, the compiler reports an error.
This catches bugs the moment you add a new enum constant.
enum class Status { ACTIVE, PAUSED, STOPPED }
fun describe(s: Status): String = when (s) {
Status.ACTIVE -> "running"
Status.PAUSED -> "paused"
Status.STOPPED -> "stopped"
}
fun main() {
println(describe(Status.PAUSED))
}Sealed Classes
A sealed class has a fixed set of subclasses known at compile time.
This lets when be exhaustive over all subtypes without else.
sealed class Shape
class Circle(val r: Double) : Shape()
class Square(val s: Double) : Shape()
fun area(shape: Shape): Double = when (shape) {
is Circle -> 3.14159 * shape.r * shape.r
is Square -> shape.s * shape.s
}
fun main() {
println(area(Square(4.0)))
}Why Exhaustiveness Helps
Exhaustive when turns missing cases into compile errors instead of silent bugs.
Add a new subclass or enum value and the compiler points to every when you must update.
Statements Are Not Forced
When used as a statement (not assigned), when does not require exhaustiveness.
But for enums and sealed types, being exhaustive anyway is good practice.
enum class Light { RED, GREEN }
fun main() {
val light = Light.RED
when (light) {
Light.RED -> println("stop")
Light.GREEN -> println("go")
}
}Combining Sealed and Data
Sealed hierarchies often use data class subclasses, giving you exhaustive matching plus smart casts to access properties.
sealed class Result
data class Ok(val value: Int) : Result()
data class Err(val message: String) : Result()
fun show(r: Result): String = when (r) {
is Ok -> "ok: " + r.value
is Err -> "error: " + r.message
}
fun main() {
println(show(Ok(42)))
println(show(Err("boom")))
}else as a Safety Net
You can still add else to a sealed or enum when, but then the compiler stops warning about new cases.
Omitting else keeps the compile-time safety, so prefer that for closed types.
enum class Coin { HEADS, TAILS }
fun flip(c: Coin): Int = when (c) {
Coin.HEADS -> 1
Coin.TAILS -> 0
}
fun main() {
println(flip(Coin.HEADS))
}Boolean and Closed Subjects
A Boolean has only two values, so listing both makes when exhaustive without else.
fun yesNo(flag: Boolean): String = when (flag) {
true -> "yes"
false -> "no"
}
fun main() {
println(yesNo(true))
println(yesNo(false))
}Putting It Together
Exhaustive when means all cases are covered:
elsecovers open subjects likeInt- Enums, sealed classes, and
Booleancan be exhaustive withoutelse - The compiler flags missing cases when you add new ones
sealed class Event
object Start : Event()
object Stop : Event()
fun handle(e: Event): String = when (e) {
is Start -> "starting"
is Stop -> "stopping"
}
fun main() {
println(handle(Start))
println(handle(Stop))
}Quick Check
Test your understanding of exhaustive when.
Recap
You learned how to make when exhaustive:
elsehandles open subjects- Enums, sealed classes, and
Booleancan be fully covered - Compile-time checks catch missing cases
That completes the When Expressions and Smart Casts course.
Frequently asked questions
Is the “Exhaustive when” lesson free?
Yes — the full text of “Exhaustive when” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “Exhaustive when”?
Cover all cases. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Exhaustive when” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- when as Expression
- Smart Casts
- is and as Operators
- Exhaustive when