推断通用超试图调用需要一个Class
作为参数现有的Java代码科特林语法,我尝试代码沿着这科特林行:从亚型
package com.example
//Acutally imported Java code in someone elses's library
abstract class JavaCode<T> {
fun doTheThing(thing: Class<JavaCode<T>>) {
//Does work
}
}
//My code
class KotlinCode : JavaCode<String>() {
fun startTheThing() {
doTheThing(KotlinCode::class.java)
} //^Type inference failed. Expected type mismatch
}
但是,这并不与编译以下错误:
Type inference failed. Expected type mismatch: inferred type is Class<KotlinCode> but Class<JavaCode<String>> was expected
所以我试图强行投(如this answer建议):
hello(GenericArgument::class.java as Class<Callable<String>>)
但是,有一个警告:
Unchecked cast: Class<KotlinCode> to Class<JavaCode<String>>
那么什么是使用正确的语法?是this有关?
因为'赎回'='可赎回',你试图声明'类GenericArgument:可赎回'? –
Stepango
这只是将预期的类型不匹配改为'Class>'。 –