您能解释一下为什么我会收到此错误吗?
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>
?
doSomething
类型的第二个参数B<T>
,所以我不明白为什么我不能传递类型的对象B<T>
?
但是items[0].b
不是类型B<T>
,而是类型B<out T>
。
如果将的第二个参数转换doSomething
为B<out T>
它将编译
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句