# Nullability check optimization

**URL:** <https://discuss.kotlinlang.org/t/nullability-check-optimization/30275>\
**Category:** Language Design\
**Created:** [April 25, 2025, 10:53am UTC](https://discuss.kotlinlang.org/t/nullability-check-optimization/30275 "2025-04-25T10:53:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vk](https://avatars.discourse-cdn.com/v4/letter/v/cab0a1/32.png) [@vk](https://discuss.kotlinlang.org/u/vk)\
**Post date:** [April 25, 2025, 10:53am UTC](https://discuss.kotlinlang.org/t/nullability-check-optimization/30275/1 "2025-04-25T10:53:30Z")

</div>

I wonder what is the reason for this kotlin compiler behavior? It would be more reasonable to make mandatory only the first nullability check in this example, as the rest of the chain is kind of guaranteed:

```auto
    interface X {
        val link: X
    }

    fun test(x: X?) {
        val linkedX: X? = x?.link?.link?.link
        val linkedX2: X? = x?.link.link.link
    }

```

But no, it is not so:

 ![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/e/e43fdc62f7c579382d31362b89ea80398500109e.png)

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [April 25, 2025, 11:15am UTC](https://discuss.kotlinlang.org/t/nullability-check-optimization/30275/2 "2025-04-25T11:15:44Z")

</div>

`?.` is not just a “nullability check”, it is an operator that returns a nullable result and works independently of other operators in the chain. `x?.link.link` can be expanded to:

```kotlin
val tmp1: X? = x?.link
val tmp2: X? = tmp1.link

```

Here, we clearly see `tmp1.link` can’t be done as `tmp1` is nullable.

In order for this to work, compiler would have to interpret the chain as a whole, not as a series of individual calls. I remember someone here on forum mentioned another language where a similar operator actually does this, so this is definitely possible. Kotlin just doesn’t do this.

---

<div class="post-metadata">

**Author:** ![vk](https://avatars.discourse-cdn.com/v4/letter/v/cab0a1/32.png) [@vk](https://discuss.kotlinlang.org/u/vk)\
**Post date:** [April 25, 2025, 11:23am UTC](https://discuss.kotlinlang.org/t/nullability-check-optimization/30275/3 "2025-04-25T11:23:33Z")

</div>

Yes, I understand that, I am concerned about ‘compiled low level code’ efficiency. I would definitely prefer ‘bytecode’ with ‘one if’, not with three.

Sad.

---

<div class="post-metadata">

**Author:** ![xyzboom](https://avatars.discourse-cdn.com/v4/letter/x/f0a364/32.png) [@xyzboom](https://discuss.kotlinlang.org/u/xyzboom)\
**Post date:** [April 26, 2025, 1:18am UTC](https://discuss.kotlinlang.org/t/nullability-check-optimization/30275/4 "2025-04-26T01:18:51Z")

</div>

Try `x?.let { it.link.link.link }`

---

<div class="post-metadata">

**Author:** ![vk](https://avatars.discourse-cdn.com/v4/letter/v/cab0a1/32.png) [@vk](https://discuss.kotlinlang.org/u/vk)\
**Post date:** [April 26, 2025, 9:51am UTC](https://discuss.kotlinlang.org/t/nullability-check-optimization/30275/5 "2025-04-26T09:51:10Z")

</div>

Yes, but this is a crutch anyway.

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [April 27, 2025, 2:09pm UTC](https://discuss.kotlinlang.org/t/nullability-check-optimization/30275/6 "2025-04-27T14:09:10Z")

</div>

> [@broot](#):
>
> `?.` is not just a “nullability check”, it is an operator that returns a nullable result and works independently of other operators in the chain. `x?.link.link` can be expanded to:
> 
> ```auto
> val tmp1: X? = x?.link
> val tmp2: X? = tmp1.link
> 
> ```
> 
> Here, we clearly see `tmp1.link` can’t be done as `tmp1` is nullable

In this example perhaps it is obvious because the type is the same, but in general that is not the case. In Kotlin we can create extension functions on nullable types. We might actually want to call such an extension function. Kotlin can’t assume that the function call should be skipped because the function itself might be able to be called on null.

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [April 27, 2025, 2:25pm UTC](https://discuss.kotlinlang.org/t/nullability-check-optimization/30275/7 "2025-04-27T14:25:30Z")

</div>

I’m not sure if I understood you correctly, but yes, we can’t treat `x?.link` as non-nullable type or skip everything that is after `?.link` in the case of null.

Kotlin could potentially do better by typing the result of `x?.link` as nullable, but with additional internal flag like: “result of ?.”. Then it would either allow `.link` on it or still require `?.link`, but at least it would use this internal information to optimize the bytecode. Such additional internal information would work only for a single expression or maybe for the local scope.

---

<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 1, 2025, 8:53pm UTC](https://discuss.kotlinlang.org/t/nullability-check-optimization/30275/8 "2025-05-01T20:53:15Z")

</div>

Did you measured the preference improvement on JVM of you proposal?  
For me, you are supposing to propose an optimization.
