Kotlin Academy · Lesson

Enums with when

Exhaustive handling.

Lesson 3 of 413 steps

Enums with when is a free Kotlin Academy lesson on CoddyKit. This is lesson 3 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.

Enums Pair Well with when

The when expression is the natural way to branch on an enum. Each constant gets its own branch.

enum class Light { RED, YELLOW, GREEN }

fun action(l: Light) = when (l) {
    Light.RED -> "Stop"
    Light.YELLOW -> "Slow"
    Light.GREEN -> "Go"
}

fun main() {
    println(action(Light.GREEN))
}

Exhaustiveness

When when is used as an expression on an enum and covers every constant, no else branch is needed. The compiler verifies completeness.

enum class Coin { HEADS, TAILS }

fun flip(c: Coin) = when (c) {
    Coin.HEADS -> "Win"
    Coin.TAILS -> "Lose"
}

fun main() {
    println(flip(Coin.HEADS))
}

The Compiler Has Your Back

If you add a new constant later but forget a branch, the compiler reports an error. This makes enums plus when very safe to evolve.

enum class Size { S, M, L }

fun label(s: Size) = when (s) {
    Size.S -> "Small"
    Size.M -> "Medium"
    Size.L -> "Large"
}

fun main() {
    println(label(Size.M))
}

Importing Constants

You can import enum members to drop the type prefix inside when, making branches shorter.

enum class Dir { UP, DOWN }

import Dir.UP
import Dir.DOWN

fun move(d: Dir) = when (d) {
    UP -> "y + 1"
    DOWN -> "y - 1"
}

fun main() {
    println(move(Dir.UP))
}

Grouping Branches

Several constants can share one branch by listing them with commas.

enum class Day { MON, TUE, WED, SAT, SUN }

fun kind(d: Day) = when (d) {
    Day.SAT, Day.SUN -> "Weekend"
    else -> "Weekday"
}

fun main() {
    println(kind(Day.SAT))
}

Returning Different Types

Each branch can produce a value; the whole when becomes that value.

enum class Tier { FREE, PRO }

fun limit(t: Tier): Int = when (t) {
    Tier.FREE -> 10
    Tier.PRO -> 1000
}

fun main() {
    println(limit(Tier.PRO))
}

when Without an Argument

You can also branch on conditions, but for enums the subject form is cleanest and enables exhaustiveness checking.

enum class Mood { HAPPY, SAD }

fun emoji(m: Mood) = when (m) {
    Mood.HAPPY -> ":)"
    Mood.SAD -> ":("
}

fun main() {
    println(emoji(Mood.HAPPY))
}

Side Effects in Branches

Branches can run statements, not just return values.

enum class Cmd { START, STOP }

fun run(c: Cmd) {
    when (c) {
        Cmd.START -> println("Starting...")
        Cmd.STOP -> println("Stopping...")
    }
}

fun main() {
    run(Cmd.START)
}

Avoid else When Possible

Using else defeats exhaustiveness checking: a new constant would silently fall into else. Prefer explicit branches for enums.

enum class State { ON, OFF }

fun text(s: State) = when (s) {
    State.ON -> "on"
    State.OFF -> "off"
}

fun main() {
    println(text(State.OFF))
}

A Realistic Router

Routing user roles to permissions is a clean use of exhaustive when.

enum class Role { ADMIN, USER, GUEST }

fun canDelete(r: Role) = when (r) {
    Role.ADMIN -> true
    Role.USER -> false
    Role.GUEST -> false
}

fun main() {
    println(canDelete(Role.ADMIN))
}

Combining with Properties

You can branch on an enum and also use its properties inside branches.

enum class Coin(val cents: Int) { PENNY(1), DIME(10) }

fun describe(c: Coin) = when (c) {
    Coin.PENNY -> "Worth ${c.cents}"
    Coin.DIME -> "Worth ${c.cents}"
}

fun main() {
    println(describe(Coin.DIME))
}

Quick Check

Test your understanding of enums with when.

Recap

You learned to combine enums with when:

  • when on an enum can be exhaustive without else.
  • The compiler enforces coverage of all constants.
  • Group constants with commas; each branch yields a value.
  • Avoid else to keep exhaustiveness safety.
enum class Light { RED, GREEN }

fun go(l: Light) = when (l) {
    Light.RED -> false
    Light.GREEN -> true
}

fun main() {
    println(go(Light.GREEN))
}
Free to start

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 “Enums with when” lesson free?

Yes — any 3 lessons of the Kotlin Academy learning path, including “Enums with 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 “Enums with when”?

Exhaustive handling. 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 3 of 4.

How long does the “Enums with 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

  1. Defining Enums
  2. Enum Properties and Methods
  3. Enums with when
  4. Enum Values and Ordinals
← Back to Kotlin Academy