# Receiving parameters in when statement

**URL:** <https://discuss.kotlinlang.org/t/receiving-parameters-in-when-statement/7842>\
**Category:** Language Design\
**Created:** [May 17, 2018, 5:28pm UTC](https://discuss.kotlinlang.org/t/receiving-parameters-in-when-statement/7842 "2018-05-17T17:28:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nwh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nwh/32/5138_2.png) [@nwh](https://discuss.kotlinlang.org/u/nwh)\
**Post date:** [May 17, 2018, 5:28pm UTC](https://discuss.kotlinlang.org/t/receiving-parameters-in-when-statement/7842/1 "2018-05-17T17:28:18Z")

</div>

**EDIT**  
This has already been discussed [here](https://discuss.kotlinlang.org/t/when-desperately-needs-it/4204/) and there is an [open issue for it](https://youtrack.jetbrains.com/issue/KT-4895).

* * *

Consider the following code for handing a nullable variable:

```kotlin
var sample: String? = ...;
sample?.let { 
  it.toUpperCase() 
} ?: run { fetchAnotherString() }

```

As far as I know, this is the best way to have two branches with a nullable variable, but I think this would be much better:

```auto
when (sample) {
  !null -> it.toUpperCase()
  else -> fetchAnotherString()
}

```

Are there limitations preventing this from being possible?

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [May 17, 2018, 6:35pm UTC](https://discuss.kotlinlang.org/t/receiving-parameters-in-when-statement/7842/2 "2018-05-17T18:35:00Z")

</div>

You should probably read [this topic](https://discuss.kotlinlang.org/t/when-desperately-needs-it/4204).

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [May 17, 2018, 6:59pm UTC](https://discuss.kotlinlang.org/t/receiving-parameters-in-when-statement/7842/3 "2018-05-17T18:59:02Z")

</div>

```kotlin
when {
  sample != null -> sample.toUpperCase()
  else -> fetchAnotherString()
}

```

or simply

```kotlin
sample?.toUpperCase() ?: fetchAnotherString()

```

---

<div class="post-metadata">

**Author:** ![nwh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nwh/32/5138_2.png) [@nwh](https://discuss.kotlinlang.org/u/nwh)\
**Post date:** [May 17, 2018, 7:01pm UTC](https://discuss.kotlinlang.org/t/receiving-parameters-in-when-statement/7842/4 "2018-05-17T19:01:04Z")

</div>

Thank you @darksnake, I believe that makes this topic redundant.

@fvasco that doesn’t work for mutable properties like `let` does.

---

<div class="post-metadata">

**Author:** ![dico.karssiens](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@dico.karssiens](https://discuss.kotlinlang.org/u/dico.karssiens)\
**Post date:** [May 24, 2018, 2:52pm UTC](https://discuss.kotlinlang.org/t/receiving-parameters-in-when-statement/7842/5 "2018-05-24T14:52:42Z")

</div>

If you like using it –

```auto
sample.let { when (it) {
    it != null -> it.toUpperCase()
    else -> fetchAnotherString()
}}
```
