【发布时间】:2020-06-11 07:06:18
【问题描述】:
你能解释一下为什么我会收到这个错误吗?
interface A
open class B<T>
class Foo: A
class Item<T : A>(
val clazz: Class<out A>,
val b: B<out A>
)
val items = mutableListOf<Item<out A>>(
Item(
Foo::class.java,
B<Foo>()
)
)
fun <T : Any?> doSomething(
type: Class<T>?,
param: B<T>
) {
// Nothing
}
fun main() {
doSomething(items[0].clazz, items[0].b) // compiler error on 2nd argument
// Type mismatch.
// Required: CapturedType(out A)
// Found: A
}
doSomething 中 B<T> 类型的第二个参数,所以我不明白为什么我无法传递 B<T> 类型的对象?
【问题讨论】:
标签: kotlin generics compiler-errors