# .filter after if else if else does not work on first if

**URL:** <https://discuss.kotlinlang.org/t/filter-after-if-else-if-else-does-not-work-on-first-if/30538>\
**Category:** Support\
**Created:** [July 3, 2025, 9:49am UTC](https://discuss.kotlinlang.org/t/filter-after-if-else-if-else-does-not-work-on-first-if/30538 "2025-07-03T09:49:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ImTema](https://avatars.discourse-cdn.com/v4/letter/i/96bed5/32.png) [@ImTema](https://discuss.kotlinlang.org/u/ImTema)\
**Post date:** [July 3, 2025, 9:49am UTC](https://discuss.kotlinlang.org/t/filter-after-if-else-if-else-does-not-work-on-first-if/30538/1 "2025-07-03T09:49:28Z")

</div>

Greetings!

Help me please undestand why this piece of code does not do what I expect it to do.

**Code snippet**

```auto
class SandboxKtTest {
    
    @Test
    fun test() {
        val a = 4
        val name = if (a < 5) {
            listOf(3,5,7)
        } else if (a >5) {
            listOf(1,2,3)
        } else {
            emptyList()
        }.filter {
            it == 3
        }
        println(name)
    }
}

```

**Expectations:**  
the result is [3]

**Actual:**  
the result is [3,5,7].

**Observations:**  
if i change `a` to 5 and 6 correspondingly it will apply `.filter`. But not for the first branch. why?

reviewing bytecode confirms this.

**Kotlin 2.1.0**

---

<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:** [July 3, 2025, 11:49am UTC](https://discuss.kotlinlang.org/t/filter-after-if-else-if-else-does-not-work-on-first-if/30538/2 "2025-07-03T11:49:21Z")

</div>

> **[A cascade \`if\` is not a single expression, hence issues with dot-qualified...](https://youtrack.jetbrains.com/issue/KT-16275/A-cascade-if-is-not-a-single-expression-hence-issues-with-dot-qualified-call-in-if-else-chain)**
>
> Example: fun printNumberSign(num: Int) { if (num ᐳ 0) { "positive" } else if (num ᐸ 0) { "negative" } else { "zero" }.let { print(it) } } fun main(args: ArrayᐸStringᐳ) { printNumberSign(1) } Prints nothing, because the inner if is the receiver for...

Simplest solution, use a `when`

---

<div class="post-metadata">

**Author:** ![klitos](https://avatars.discourse-cdn.com/v4/letter/k/258eb7/32.png) [@klitos](https://discuss.kotlinlang.org/u/klitos)\
**Post date:** [July 4, 2025, 9:31am UTC](https://discuss.kotlinlang.org/t/filter-after-if-else-if-else-does-not-work-on-first-if/30538/3 "2025-07-04T09:31:44Z")

</div>

It’s because your cascaded `if` expression is equivalent to this:

```
    val name = if (a < 5) {
        listOf(3,5,7)
    } else {
        if (a >5) {
            listOf(1,2,3)
        } else {
            emptyList()
        }.filter {
            it == 3
        }
    }

```

It is easy to understand why the `.filter` binds only to the innermost `if` expression when you consider this:

```
val s = if (keepOriginal) str else str.upperCase()

```

Here, you wouldn’t expect `.upperCase()` to be applied to the whole `if` expression, but only to the `else` part. Similarly, the `.filter` above doesn’t apply to the whole outer `if` expression, but only to the `else` part of the outer `if`.
