Compiler give me 'Type mismatch', 1.3.41

Those code works well everywhere.

class Combine2LiveData<A : Any, B : Any, R : Any>(
        sourceA: LiveData<A>,
        sourceB: LiveData<B>,
        private val block: (a: A?, b: B?) -> R?
) : MediatorLiveData<R>() { /* ... */ }
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 }
fun <X : Any> LiveData<X>.distinctUntilChanged(): MutableLiveData<X> {

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


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


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

Hi @lymoge,

I think your first error is because {first} is defined as a function, not an extension property, 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?

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.

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 about the new inference.

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:

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.

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