0
0
Kotlinprogramming~10 mins

Enum with when exhaustive check in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Enum with when exhaustive check
Start
Define enum
Use when with enum
Check all enum cases
All cases covered?
NoCompiler error
Yes
Execute matched branch
End
The program defines an enum and uses a when expression to handle all enum values. The compiler checks if all enum cases are covered, ensuring no case is missed.
Execution Sample
Kotlin
enum class Direction { NORTH, SOUTH, EAST, WEST }

fun describe(dir: Direction) = when(dir) {
  Direction.NORTH -> "Up"
  Direction.SOUTH -> "Down"
  Direction.EAST -> "Right"
  Direction.WEST -> "Left"
}
This code defines a Direction enum and a function that returns a string description for each direction using an exhaustive when expression.
Execution Table
StepInput (dir)Condition CheckedBranch TakenOutput
1Direction.NORTHdir == NORTH?Yes"Up"
2Direction.SOUTHdir == NORTH? No; dir == SOUTH?Yes"Down"
3Direction.EASTdir == NORTH? No; dir == SOUTH? No; dir == EAST?Yes"Right"
4Direction.WESTdir == NORTH? No; dir == SOUTH? No; dir == EAST? No; dir == WEST?Yes"Left"
5Direction.UNKNOWN (hypothetical)No matching branchCompiler errorNo output
💡 Execution stops after matching the input direction to a branch; if any enum case is missing, compiler error prevents running.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
dirNORTHSOUTHEASTWESTUNKNOWN (hypothetical)N/A
outputN/A"Up""Down""Right""Left"N/A
Key Moments - 2 Insights
Why does the compiler give an error if I forget one enum case in the when expression?
Because the when expression is exhaustive for enums, the compiler checks all enum values are handled (see execution_table row 5). Missing a case causes a compile-time error to prevent unexpected behavior.
What happens if I add an else branch in the when expression?
Adding else makes the when expression non-exhaustive for enums, so the compiler stops checking all enum cases. This can hide missing cases and is not recommended for enums.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what output is produced when dir is Direction.EAST?
A"Up"
B"Right"
C"Down"
D"Left"
💡 Hint
Check execution_table row 3 for dir == EAST and the output column.
At which step does the compiler error occur if a case is missing?
AStep 5
BStep 4
CStep 1
DNo error occurs
💡 Hint
See execution_table row 5 describing the hypothetical missing case and compiler error.
If you add an else branch to the when, what changes in the compiler check?
ACompiler enforces all enum cases still
BCompiler throws runtime error
CCompiler stops enforcing exhaustive check
DCompiler requires more enum cases
💡 Hint
Refer to key_moments second question about else branch effect.
Concept Snapshot
enum class Direction { ... }
fun describe(dir: Direction) = when(dir) {
  Direction.NORTH -> "Up"
  Direction.SOUTH -> "Down"
  Direction.EAST -> "Right"
  Direction.WEST -> "Left"
}

- when with enum must cover all cases
- compiler enforces exhaustive check
- no else needed or allowed for full coverage
Full Transcript
This example shows how Kotlin uses enums with when expressions. The enum Direction has four values. The function describe uses when to return a string for each direction. The compiler checks that all enum cases are handled, so no case is missed. If a case is missing, the compiler gives an error. Adding an else branch disables this check. The execution table shows how each input direction matches a branch and produces output. The variable tracker shows how the input and output change step by step. Key moments explain why exhaustive checks matter and how else affects them. The visual quiz tests understanding of outputs and compiler behavior.