# Allow to declare directly \`val\` in the \`if\` statement and use in body

**URL:** <https://discuss.kotlinlang.org/t/allow-to-declare-directly-val-in-the-if-statement-and-use-in-body/1996>\
**Category:** Language Design\
**Created:** [September 28, 2016, 6:22pm UTC](https://discuss.kotlinlang.org/t/allow-to-declare-directly-val-in-the-if-statement-and-use-in-body/1996 "2016-09-28T18:22:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![elect](https://avatars.discourse-cdn.com/v4/letter/e/e480ec/32.png) [@elect](https://discuss.kotlinlang.org/u/elect)\
**Post date:** [September 28, 2016, 6:22pm UTC](https://discuss.kotlinlang.org/t/allow-to-declare-directly-val-in-the-if-statement-and-use-in-body/1996/1 "2016-09-28T18:22:27Z")

</div>

First of all, Kotlin is awesome!

Second, it’d be cool if we could have something similar to smart cast, that is declaring a `val` (thread safe) inside the `if` statement and use it directly in the body

`if( val email = client?.personalInfo?.email != null && message!=null) mailer.sendMessage(email, message)`

---

<div class="post-metadata">

**Author:** ![jhoak](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@jhoak](https://discuss.kotlinlang.org/u/jhoak)\
**Post date:** [September 29, 2016, 1:08am UTC](https://discuss.kotlinlang.org/t/allow-to-declare-directly-val-in-the-if-statement-and-use-in-body/1996/2 "2016-09-29T01:08:29Z")

</div>

I’m not really a language expert, but that just seems harder to read while you’re only saving one line of code. I’d prefer putting the email initialization right above that.

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [September 29, 2016, 11:06am UTC](https://discuss.kotlinlang.org/t/allow-to-declare-directly-val-in-the-if-statement-and-use-in-body/1996/3 "2016-09-29T11:06:33Z")

</div>

You can use the `let` function for this purpose:

```
client?.personalInfo?.email?.let { email -> 
  if (message != null) mailer.sendMessage(email, message)
}

```

---

<div class="post-metadata">

**Author:** ![Denis535](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@Denis535](https://discuss.kotlinlang.org/u/Denis535)\
**Post date:** [October 19, 2025, 9:49pm UTC](https://discuss.kotlinlang.org/t/allow-to-declare-directly-val-in-the-if-statement-and-use-in-body/1996/4 "2025-10-19T21:49:39Z")

</div>

That would be wonderful:

**if (val value = this.Value; value is Type) {**

**}**

---

<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:** [October 19, 2025, 9:55pm UTC](https://discuss.kotlinlang.org/t/allow-to-declare-directly-val-in-the-if-statement-and-use-in-body/1996/5 "2025-10-19T21:55:28Z")

</div>

Just use a let:

```kotlin
this.Value.let { value ->
  if (value !is Type) return@let
  ...
}

```

---

<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:** [October 19, 2025, 10:14pm UTC](https://discuss.kotlinlang.org/t/allow-to-declare-directly-val-in-the-if-statement-and-use-in-body/1996/6 "2025-10-19T22:14:30Z")

</div>

In this specific case, I would write:

```auto
this.Value
  .let{ it as? Type }
  ?.let { value -> ... }

```

---

<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:** [October 19, 2025, 10:15pm UTC](https://discuss.kotlinlang.org/t/allow-to-declare-directly-val-in-the-if-statement-and-use-in-body/1996/7 "2025-10-19T22:15:17Z")

</div>

Or even:

```kotlin
(this.Value as? Type)?.let { value ->
  ...
}
```

---

<div class="post-metadata">

**Author:** ![Varia](https://avatars.discourse-cdn.com/v4/letter/v/b9e5f3/32.png) [@Varia](https://discuss.kotlinlang.org/u/Varia)\
**Post date:** [October 20, 2025, 10:56am UTC](https://discuss.kotlinlang.org/t/allow-to-declare-directly-val-in-the-if-statement-and-use-in-body/1996/8 "2025-10-20T10:56:54Z")

</div>

Or even

```kotlin
val value = this.Value; if (value is Type) {
    …
}

```

I am very much in favor of preferring the most basic syntax unless there are significant advantages to the more creative alternatives.

---

<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:** [October 20, 2025, 8:38pm UTC](https://discuss.kotlinlang.org/t/allow-to-declare-directly-val-in-the-if-statement-and-use-in-body/1996/9 "2025-10-20T20:38:03Z")

</div>

How about a little helper function?

```kotlin
inline fun <reified T> Any.ifType(crossinline body : (T) -> Unit) {
    if (this is T) {
        body(this)
    }
}

```

Usage: `this.Value.ifType<Type>{ v -> ... }`
