# Extension to ?.let syntax

**URL:** <https://discuss.kotlinlang.org/t/extension-to-let-syntax/16408>\
**Category:** Language Design\
**Created:** [February 19, 2020, 11:15pm UTC](https://discuss.kotlinlang.org/t/extension-to-let-syntax/16408 "2020-02-19T23:15:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nstublen](https://avatars.discourse-cdn.com/v4/letter/n/bbce88/32.png) [@nstublen](https://discuss.kotlinlang.org/u/nstublen)\
**Post date:** [February 19, 2020, 11:15pm UTC](https://discuss.kotlinlang.org/t/extension-to-let-syntax/16408/1 "2020-02-19T23:15:51Z")

</div>

I often use `?.let {}` for simple `null` checks, but I’m wondering if the following syntax would ever be feasible in the language:

```
myVar?.let {
    it.doSomething()
} else {
    // Do something else because myVar is null
}

```

---

<div class="post-metadata">

**Author:** ![arocnies](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arocnies/32/5143_2.png) [@arocnies](https://discuss.kotlinlang.org/u/arocnies)\
**Post date:** [February 19, 2020, 11:33pm UTC](https://discuss.kotlinlang.org/t/extension-to-let-syntax/16408/2 "2020-02-19T23:33:49Z")

</div>

Why not use this?:

```kotlin
if (myVar != null) {
    myVar.doSomething()
} else {
    // Do something else because myVar is null
}

```

It’s a common Kotlin code-smell to replace a short null check with `?.let {}`.  
Many people (myself included) experience a `?.let {}` phase when learning Kotlin.

---

<div class="post-metadata">

**Author:** ![nstublen](https://avatars.discourse-cdn.com/v4/letter/n/bbce88/32.png) [@nstublen](https://discuss.kotlinlang.org/u/nstublen)\
**Post date:** [February 20, 2020, 12:59am UTC](https://discuss.kotlinlang.org/t/extension-to-let-syntax/16408/3 "2020-02-20T00:59:45Z")

</div>

The main use would be when `myVar` is mutable and Kotlin tells you the value may have changed, so it can’t smartcast inside the if block.

---

<div class="post-metadata">

**Author:** ![pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@pit](https://discuss.kotlinlang.org/u/pit)\
**Post date:** [February 20, 2020, 5:37am UTC](https://discuss.kotlinlang.org/t/extension-to-let-syntax/16408/4 "2020-02-20T05:37:22Z")

</div>

Instead of `let` you can use `also` and instead of `else` the Elvis operator:

```
myVar?.also {
    it.doSomething()
} ?: {
    // Do something else because myVar is null
}

```

---

<div class="post-metadata">

**Author:** ![derektom14](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/derektom14/32/7411_2.png) [@derektom14](https://discuss.kotlinlang.org/u/derektom14)\
**Post date:** [February 20, 2020, 6:46am UTC](https://discuss.kotlinlang.org/t/extension-to-let-syntax/16408/5 "2020-02-20T06:46:24Z")

</div>

Note that you will need to use `run { ... }` to execute the code withi the code block, otherwise it’s just a lambda.

---

<div class="post-metadata">

**Author:** ![tieskedh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/tieskedh/32/2816_2.png) [@tieskedh](https://discuss.kotlinlang.org/u/tieskedh)\
**Post date:** [February 20, 2020, 10:00am UTC](https://discuss.kotlinlang.org/t/extension-to-let-syntax/16408/6 "2020-02-20T10:00:13Z")

</div>

You are right, as the question states doSomething instead of getSomething.  
When the code would say getSomething, your approach doesn’t work anymore.

The best approach would then be to assign the receiver to a local variable.

You can also write the following function:

```
inline fun <T : Any, S> T?.mapNullable(
    notNull: (T) -> S,
    onNull : () -> S
) = when(this){
   null -> onNull()
   else -> notNull(this)
}

```

Then you can rewrite

```
myVar?.let {
    it.computePresentValue()
} else {
    it.computeAbsentValue()
}

```

to

```
val t = myVar.mapNullable(
    notNull = { it.computePresentValue() },
    onNull = { it.computeAbsentValue() }
)

```

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [February 20, 2020, 1:14pm UTC](https://discuss.kotlinlang.org/t/extension-to-let-syntax/16408/7 "2020-02-20T13:14:23Z")

</div>

> [@tieskedh](#):
>
> ```
> val t = myVar.mapNullable(
> notNull = { it.computePresentValue() },
> onNull = { it.computeAbsentValue() }
> )
> 
> ```

I fail to se how that is better than

```
val t = when (val it = myVar) {
    null -> computeAbsentValue()
    else -> it.computeAbsentValue()
}

```

---

<div class="post-metadata">

**Author:** ![tieskedh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/tieskedh/32/2816_2.png) [@tieskedh](https://discuss.kotlinlang.org/u/tieskedh)\
**Post date:** [February 20, 2020, 1:15pm UTC](https://discuss.kotlinlang.org/t/extension-to-let-syntax/16408/8 "2020-02-20T13:15:16Z")

</div>

It isn’t. Yours is best.  
Just didn’t think about it…
