# Compiler give me 'Type mismatch', 1.3.41

**URL:** <https://discuss.kotlinlang.org/t/compiler-give-me-type-mismatch-1-3-41/13571>\
**Category:** Support\
**Created:** [July 22, 2019, 2:23am UTC](https://discuss.kotlinlang.org/t/compiler-give-me-type-mismatch-1-3-41/13571 "2019-07-22T02:23:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lymoge](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@lymoge](https://discuss.kotlinlang.org/u/lymoge)\
**Post date:** [July 22, 2019, 2:23am UTC](https://discuss.kotlinlang.org/t/compiler-give-me-type-mismatch-1-3-41/13571/1 "2019-07-22T02:23:31Z")

</div>

Those code works well everywhere.

```auto
class Combine2LiveData<A : Any, B : Any, R : Any>(
        sourceA: LiveData<A>,
        sourceB: LiveData<B>,
        private val block: (a: A?, b: B?) -> R?
) : MediatorLiveData<R>() { /* ... */ }

```

```auto
fun <A : Any, B : Any> bothNotNull(): (a: A?, b: B?) -> Pair<A, B>? {
    return { a, b -> if (a == null || b == null) null else a to b }
}

```

```auto
fun <X : Any> LiveData<X>.distinctUntilChanged(): MutableLiveData<X> {
 //
}

```

Butwhen I do this, I got error when compiling:  
`Function invocation 'first(...)' expected`

```auto
        Combine2LiveData(
                MutableLiveData<Int>(),
                MutableLiveData<String>(),
                bothNotNull()
        ).distinctUntilChanged().observeNullable(this){
            it?.first
        }

```

Or when I do this, I got:  
`Type mismatch: inferred type is Combine2LiveData<Int!, String!, Any> but LiveData<Pair<Int, String>> was expected`

```auto
        Combine2LiveData(
                MutableLiveData<Int>(),
                MutableLiveData<String>(),
                bothNotNull()
        ).distinctUntilChanged<Pair<Int,String>>()

```

I have to add generic type on `bothNotNull` to avoid this error, but IDE give me warning.

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

---

<div class="post-metadata">

**Author:** ![HughG](https://avatars.discourse-cdn.com/v4/letter/h/35a633/32.png) [@HughG](https://discuss.kotlinlang.org/u/HughG)\
**Post date:** [July 22, 2019, 7:43am UTC](https://discuss.kotlinlang.org/t/compiler-give-me-type-mismatch-1-3-41/13571/2 "2019-07-22T07:43:17Z")

</div>

Hi @lymoge,

I think your first error is because {first} is defined as a function, not an extension property [https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/first.html](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/first.html), so you need to do {it.?first()}.

The second error … I don’t know, it looks right to me but I’m not in front of an IDE to try it out. Maybe try creating the Combine2LiveData instance as a {val combine: LiveData\<Pair\<Int, String\>\>} separately and see what type-inference errors you get for that?

---

<div class="post-metadata">

**Author:** ![lymoge](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@lymoge](https://discuss.kotlinlang.org/u/lymoge)\
**Post date:** [July 22, 2019, 7:58am UTC](https://discuss.kotlinlang.org/t/compiler-give-me-type-mismatch-1-3-41/13571/3 "2019-07-22T07:58:35Z")

</div>

That is a `Pair` type, so there is no error in IDE, but compiler gives me an error. I think this is because compiler not so smart to detect that generic type. it thinks that is a `Any` type. but when I specify it as Pair type, I got that warning.

---

<div class="post-metadata">

**Author:** ![Alexey.Belkov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/alexey.belkov/32/5215_2.png) [@Alexey.Belkov](https://discuss.kotlinlang.org/u/Alexey.Belkov)\
**Post date:** [July 22, 2019, 8:16am UTC](https://discuss.kotlinlang.org/t/compiler-give-me-type-mismatch-1-3-41/13571/4 "2019-07-22T08:16:36Z")

</div>

Looks like the new inference enabled in the IDE by default infers types more correctly. To make the IDE/compiler behavior consistent please disable the new type inference in “Preferences | Build, Execution, Deployment | Compiler | Kotlin Compiler” in the IDE.

See also [https://blog.jetbrains.com/kotlin/2019/06/kotlin-1-3-40-released/](https://blog.jetbrains.com/kotlin/2019/06/kotlin-1-3-40-released/) about the new inference.

---

<div class="post-metadata">

**Author:** ![lymoge](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@lymoge](https://discuss.kotlinlang.org/u/lymoge)\
**Post date:** [July 22, 2019, 8:31am UTC](https://discuss.kotlinlang.org/t/compiler-give-me-type-mismatch-1-3-41/13571/5 "2019-07-22T08:31:32Z")

</div>

I turned it off just now, now I got a warning in IDE if specify the generic type, got an error in IDE if handle the warning with remove generic type.

Same problem with little code:

 ![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/7/7006a652e62dcb856a9cc7ada2c687e60c5a95c7.png)  
 ![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/2/2ca5576e1bfdb390a6c3d2d4a8e2fdbe1567709f.png)

---

<div class="post-metadata">

**Author:** ![Alexey.Belkov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/alexey.belkov/32/5215_2.png) [@Alexey.Belkov](https://discuss.kotlinlang.org/u/Alexey.Belkov)\
**Post date:** [July 22, 2019, 8:53am UTC](https://discuss.kotlinlang.org/t/compiler-give-me-type-mismatch-1-3-41/13571/7 "2019-07-22T08:53:44Z")

</div>

Can’t reproduce with your minimized example. Please try to restart the IDE. If the new type inference is turned off in the IDE and you’re not compiling the project with the compiler argument `-XXLanguage:+NewInference`, then there should be no inspection to remove explicit type arguments.

---

<div class="post-metadata">

**Author:** ![lymoge](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@lymoge](https://discuss.kotlinlang.org/u/lymoge)\
**Post date:** [July 22, 2019, 9:00am UTC](https://discuss.kotlinlang.org/t/compiler-give-me-type-mismatch-1-3-41/13571/8 "2019-07-22T09:00:44Z")

</div>

This is how it looks like when turned off that option.

 ![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/5/55d234b1580dbb2ac660fa6062de4518fd5d6624.png) ![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/f/fbe3cf9e71d371049cfff32e516c32e38aa9fce4.png)
