Exhaustive when
Cover all cases.
Exhaustive when is a free Kotlin Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
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.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Exhaustive when” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “Exhaustive when”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
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