# Enchancements for when statement

**URL:** <https://discuss.kotlinlang.org/t/enchancements-for-when-statement/18189>\
**Category:** Language Design\
**Created:** [June 22, 2020, 3:51pm UTC](https://discuss.kotlinlang.org/t/enchancements-for-when-statement/18189 "2020-06-22T15:51:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterhenryd](https://avatars.discourse-cdn.com/v4/letter/p/90db22/32.png) [@peterhenryd](https://discuss.kotlinlang.org/u/peterhenryd)\
**Post date:** [June 22, 2020, 3:51pm UTC](https://discuss.kotlinlang.org/t/enchancements-for-when-statement/18189/1 "2020-06-22T15:51:54Z")

</div>

```auto
when (test(_) == MyThing.ONE) {
   "hello" -> { /* ... */ }
   "hi" -> { /* ... */ }
   "greetings" -> { /* ... */ }
   "yo" -> { /* ... */ }
}

```

Now for each entry, it would be evaluated with the argument from `_`.  
If you broke it down to another when statement, it would look like this.

```auto
when (MyThing.ONE) {
   test("hello") -> { /* ... */ }
   test("hi") -> { /* ... */ }
   test("greetings") -> { /* ... */ }
   test("yo") -> { /* ... */ }
}

```

This feature could perhaps go into multiple when arguments too.

```auto
when (test(_).func(_) == MyThing.ONE) {
   "hello", 30 -> { /* ... */ }
   "hi", 20 -> { /* ... */ }
   "greetings", 50 -> { /* ... */ }
   "yo", 10 -> { /* ... */ }
}

```

---

<div class="post-metadata">

**Author:** ![benwoodworth](https://avatars.discourse-cdn.com/v4/letter/b/ad7895/32.png) [@benwoodworth](https://discuss.kotlinlang.org/u/benwoodworth)\
**Post date:** [June 22, 2020, 5:19pm UTC](https://discuss.kotlinlang.org/t/enchancements-for-when-statement/18189/2 "2020-06-22T17:19:15Z")

</div>

There’s a discussion about adding pattern matching here that you might be interested in:

> <https://github.com/Kotlin/KEEP/pull/213>
>
> \## Proposal
> The goal of this proposal is to bring full blown pattern matching t…o Kotlin. 
> 
> Pattern matching is a tried and tested feature of many languages, such as Haskell, Rust, Scala, Ruby, and in the near future, Java.
> 
> In a nutshell, it is a quick and intuitive way of checking whether an object is of some type, and to extract and check its contents. The propsal aims to do this through the existing \`is\` and destructuring \`componentN()\` semantics.
> 
> Pattern matching is a major addition to a language, and as such several aspects of the proposal are up to debate. Discussion and contributions are welcome. 
> 
> Here is a short example of what Kotlin with pattern matching could look like:
> 
> \`\`\`kotlin
> sealed class Download
> data class App(val name: String, val developer: Person) : Download()
> data class Movie(val title: String, val director: Person) : Download()
> val download: Download = // ...
> 
> // Without pattern matching:
> val result = when(download) {
> is App -\> {
> val (name, dev) = download
> when(dev) {
> is Person -\> 
> if(dev.name == "Alice") "Alice's app $name" else "Not by Alice"
> else -\> "Not by Alice"
> }
> }
> is Movie -\> {
> val (title, diretor) = download
> if (director.name == "Alice") {
> "Alice's movie $title"
> } else {
> "Not by Alice"
> }
> }
> }
> 
> // With pattern matching:
> val result = when(download) {
> is App(name, Person("Alice", \_)) -\> "Alice's app $name"
> is Movie(title, Person("Alice", \_)) -\> "Alice's movie $title"
> is App, Movie -\> "Not by Alice"
> }
> \`\`\`
> 
> For more details, you can read a rendered version of the proposal \[here\](https://github.com/Cottand/KEEP/blob/pattern-matching/proposals/pattern-matching.md).

There’s also a new #pattern-matching channel on the Kotlin Slack
