Это относится к Kotlin типам variance
.
Тип MutableList<T>
является инвариантным в его типе T
, так что вы можете ' * присваивать MutableList<InterfaceType>
MutableList<TypeA>
.
Чтобы иметь возможность назначить его, поскольку InterfaceType
является супертипом TypeA
, вам потребуется класс ковариантный в его типе (например, List
).
interface InterfaceType {}
open class ConcreteImpl: InterfaceType {}
class TypeA: ConcreteImpl() {}
fun test() {
var interfaceTypeList: List<InterfaceType> = mutableListOf()
var typeAList: MutableList<TypeA> = mutableListOf()
interfaceTypeList = typeAList
}
В противном случае вы должны сделать неконтролируемое приведение к MutableList<InterfaceType>
.
```kotlin
interface InterfaceType {}
open class ConcreteImpl: InterfaceType {}
class TypeA: ConcreteImpl() {}
fun test() {
var interfaceTypeList: MutableList<InterfaceType> = mutableListOf()
var typeAList: MutableList<TypeA> = mutableListOf()
// Unchecked cast.
interfaceTypeList = typeAList as MutableList<InterfaceType>
}