# \[SOLVED\] Improved exhaustiveness checks for when expressions with sealed classes﻿ in Kotlin 2.1.0

**URL:** <https://discuss.kotlinlang.org/t/solved-improved-exhaustiveness-checks-for-when-expressions-with-sealed-classes-in-kotlin-2-1-0/30622>\
**Category:** Language Design\
**Created:** [July 26, 2025, 10:05pm UTC](https://discuss.kotlinlang.org/t/solved-improved-exhaustiveness-checks-for-when-expressions-with-sealed-classes-in-kotlin-2-1-0/30622 "2025-07-26T22:05:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Landei](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/landei/32/9407_2.png) [@Landei](https://discuss.kotlinlang.org/u/Landei)\
**Post date:** [July 26, 2025, 10:05pm UTC](https://discuss.kotlinlang.org/t/solved-improved-exhaustiveness-checks-for-when-expressions-with-sealed-classes-in-kotlin-2-1-0/30622/1 "2025-07-26T22:05:49Z")

</div>

My company code moved finally up to Kotlin 2.1.0 (yay!) and I wanted to give a quick demo of the new features. The `else` branch in `when` when it was logically not needed was always bothering me, so I’m glad there is some improvement. However, this seems to work only one level deep:

```kotlin
// Kotlin 2.1.0
sealed interface Animal {
    interface Bird: Animal
    class Raven: Bird
    class Hawk: Bird
    interface Mammal: Animal
    class Lion: Mammal
    class Panda: Mammal
}

fun main() {
    val animal: Animal = listOf(Raven(), Hawk(), Lion(), Panda()).shuffled().first()
    println("selected ${animal.javaClass.simpleName}")

    when(animal) {
        is Bird -> println("flies")
        is Lion -> println("roars")
        is Panda -> println("eats shoots and leaves")
        // wants still an else branch...
    }
}

```

The code works as advertised when there is a `Mammal` branch instead of the sub class branches of `Mammal`, which is already a huge step in the right direction, but I don’t get why the problem wasn’t solved completely when they were already working on it. It doesn’t seem to be too difficult, right?

What are your thoughts?

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [July 26, 2025, 10:28pm UTC](https://discuss.kotlinlang.org/t/solved-improved-exhaustiveness-checks-for-when-expressions-with-sealed-classes-in-kotlin-2-1-0/30622/2 "2025-07-26T22:28:57Z")

</div>

`Bird` and `Mammal` can both be implemented by classes outside your module. What you actually want is to make them both `sealed interface`s as well, then the code works as you expect

---

<div class="post-metadata">

**Author:** ![Skater901](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/skater901/32/10163_2.png) [@Skater901](https://discuss.kotlinlang.org/u/Skater901)\
**Post date:** [July 27, 2025, 4:53am UTC](https://discuss.kotlinlang.org/t/solved-improved-exhaustiveness-checks-for-when-expressions-with-sealed-classes-in-kotlin-2-1-0/30622/3 "2025-07-27T04:53:09Z")

</div>

Maybe I’m missing something, but I thought you only needed to cover all cases when `when` is used as an expression. If it’s just used as a statement, I thought the compiler didn’t care if you cover all branches or not.

---

<div class="post-metadata">

**Author:** ![Landei](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/landei/32/9407_2.png) [@Landei](https://discuss.kotlinlang.org/u/Landei)\
**Post date:** [July 27, 2025, 7:07am UTC](https://discuss.kotlinlang.org/t/solved-improved-exhaustiveness-checks-for-when-expressions-with-sealed-classes-in-kotlin-2-1-0/30622/4 "2025-07-27T07:07:11Z")

</div>

Thank you, that makes sense. I made somehow the wrong assumption that a class inside a sealed class is automatically sealed itself.

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [July 27, 2025, 2:28pm UTC](https://discuss.kotlinlang.org/t/solved-improved-exhaustiveness-checks-for-when-expressions-with-sealed-classes-in-kotlin-2-1-0/30622/5 "2025-07-27T14:28:41Z")

</div>

It’s a new [change](https://youtrack.jetbrains.com/issue/KTLC-99/Make-when-statements-with-enum-sealed-and-Boolean-subjects-exhaustive-by-default)!

---

<div class="post-metadata">

**Author:** ![Skater901](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/skater901/32/10163_2.png) [@Skater901](https://discuss.kotlinlang.org/u/Skater901)\
**Post date:** [July 28, 2025, 4:42am UTC](https://discuss.kotlinlang.org/t/solved-improved-exhaustiveness-checks-for-when-expressions-with-sealed-classes-in-kotlin-2-1-0/30622/6 "2025-07-28T04:42:19Z")

</div>

Oh actually I do think I remember seeing something about this… not sure I love this change, but oh well.
