# Allow \_ in exhaustive when

**URL:** <https://discuss.kotlinlang.org/t/allow-in-exhaustive-when/7823>\
**Category:** Language Design\
**Created:** [May 16, 2018, 7:31am UTC](https://discuss.kotlinlang.org/t/allow-in-exhaustive-when/7823 "2018-05-16T07:31:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alamothe](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@alamothe](https://discuss.kotlinlang.org/u/alamothe)\
**Post date:** [May 16, 2018, 7:31am UTC](https://discuss.kotlinlang.org/t/allow-in-exhaustive-when/7823/1 "2018-05-16T07:31:45Z")

</div>

The only way to make `when` exhaustive (for sealed classes) is to use it as an expression. The problem is, a lot of times there is no result (or in other words, result is `Unit`).

So I end up writing:

```auto
val dummy = when (x) {
  is A -> doA()
  is B -> doB()
  is C -> doC()
}

```

(the other option is to refactor as a separate function, which is inconvenient in many cases)

Unfortunately, this bothers the compiler because `dummy` is unused.

My suggestion is to allow special variable `_` in exhaustive `when`

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [May 16, 2018, 8:19am UTC](https://discuss.kotlinlang.org/t/allow-in-exhaustive-when/7823/2 "2018-05-16T08:19:43Z")

</div>

I know why you want this. Using `when` as an expression is the only way (right now) to ensure that the compiler checks that it is exhaustive.  
That being said I don’t like your suggestion to handle it as it feels like a hack and not like a feature. Instead I would take a look at this [topic](https://discuss.kotlinlang.org/t/algebraic-data-types-are-not-exhaustive/1857). There is a discussion about the same problem. I like @fvasco’s suggestion

```kotlin
when(x) {
    is A -> doA()
    is B -> doB()
    !else
}

```

---

<div class="post-metadata">

**Author:** ![alamothe](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@alamothe](https://discuss.kotlinlang.org/u/alamothe)\
**Post date:** [May 16, 2018, 9:39pm UTC](https://discuss.kotlinlang.org/t/allow-in-exhaustive-when/7823/3 "2018-05-16T21:39:20Z")

</div>

Thanks!

I like `.let{}`, looks better than my approach 🙂

I will switch to using that until there is a better (official?) solution

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [May 17, 2018, 9:39am UTC](https://discuss.kotlinlang.org/t/allow-in-exhaustive-when/7823/4 "2018-05-17T09:39:57Z")

</div>

Note that in many cases the when clause is the top level of the function. In that case you can use the expression body to make it elegant:

```auto
  fun doSomething(param:Sealed):Unit = when (param) {
    is Sealed.A -> println("A")
    is Sealed.B -> println("B")
  }

```

The return type declaration is not necessary in this case, but can prevent all kinds of confusion.

---

<div class="post-metadata">

**Author:** ![ligi](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ligi/32/7186_2.png) [@ligi](https://discuss.kotlinlang.org/u/ligi)\
**Post date:** [January 22, 2020, 5:46pm UTC](https://discuss.kotlinlang.org/t/allow-in-exhaustive-when/7823/5 "2020-01-22T17:46:54Z")

</div>

would really love to see something like @Wasabi375 posted as a language feature - so i opened a KEEP issue for it to maybe push it a bit: [unreachable() · Issue #204 · Kotlin/KEEP · GitHub](https://github.com/Kotlin/KEEP/issues/204)
