MutableList not accepting AbstractList from external lib


#1

Trying to compile this:

package somepkg

import org.mapdb.IndexTreeList

object KotlinTemp {
    val temp: IndexTreeList<String> = TODO()
    val temp2: MutableList<String> = temp
}

And getting:

e: redacted\src\main\kotlin\somepkg\KotlinTemp.kt: (7, 38): Type mismatch: inferred type is IndexTreeList<String> but MutableList<String> was expected

Why is this happening? I verified that IndexTreeList extends java.util.AbstractList (even by decompiling the dependency JAR). Unfortunately I cannot reduce the test case any further including attempting to replicate with local code only. The version of the lib I am using is 3.0.5 and was built using Kotlin 1.0.7 …is there a bytecode incompatibility?


#2

It looks like IndexTreeList<T> extends AbstractList<T?> thus making element type ultimately nullable. Because of that you cannot assign this instance to a list of non-nullable elements.

Try declaring temp2 as MutableList<String?>.


#3

Ah, yes, missed that. Thanks! I am afraid for performance reasons I’m going to have to use a type-erased cast to the not-null version instead of a runtime mapNotNull. Open to other suggestions (I believe the original author did it wrong by not staying true to the generic, but meh).