0Pricing
Kotlin Academy · Lesson

Enums with when

Exhaustive handling.

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

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))
}

Frequently asked questions

Is the “Enums with when” lesson free?

Yes — the full text of “Enums with 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 “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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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