# Why does this not compile?

**URL:** <https://discuss.kotlinlang.org/t/why-does-this-not-compile/23075>\
**Category:** Support\
**Created:** [September 30, 2021, 11:25am UTC](https://discuss.kotlinlang.org/t/why-does-this-not-compile/23075 "2021-09-30T11:25:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anirudhgupta281998](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@anirudhgupta281998](https://discuss.kotlinlang.org/u/anirudhgupta281998)\
**Post date:** [September 30, 2021, 11:25am UTC](https://discuss.kotlinlang.org/t/why-does-this-not-compile/23075/1 "2021-09-30T11:25:59Z")

</div>

```auto
fun <K, V> Array<Pair<K, V?>>.filterNotNullBySecond(): List<Pair<K, V>> {
    val destination = ArrayList<Pair<K, V>>()
    for (element in this) {
        val second = element.second
        if (second != null) destination.add(Pair(element.first, second))
    }
    return destination
}

fun <K, V> mapOfNonNullValues(vararg pairs: Pair<K, V?>): Map<K, V> =
    mapOf(*pairs.filterNotNullBySecond().toTypedArray())

```

I get an error on compiling:

```auto
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: public fun <K, V> Array<Pair<TypeVariable(K), TypeVariable(V)?>>.filterNotNullBySecond(): List<Pair<TypeVariable(K), TypeVariable(V)>> defined in root package in file File.kt

```

---

<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:** [September 30, 2021, 2:51pm UTC](https://discuss.kotlinlang.org/t/why-does-this-not-compile/23075/2 "2021-09-30T14:51:43Z")

</div>

This is because `filterNotNullBySecond()` may potentially modify the contents of the array and `pairs` should not be modified. Even if in fact `filterNotNullBySecond()` does not modify it, we need to inform the compiler about that by specifying the variance:

```kotlin
fun <K, V> Array<out Pair<K, V?>>.filterNotNullBySecond()

```

---

<div class="post-metadata">

**Author:** ![anirudhgupta281998](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@anirudhgupta281998](https://discuss.kotlinlang.org/u/anirudhgupta281998)\
**Post date:** [September 30, 2021, 5:28pm UTC](https://discuss.kotlinlang.org/t/why-does-this-not-compile/23075/3 "2021-09-30T17:28:48Z")

</div>

Oh okay… thanks for the answer…
