尝试调用期望 aClass
作为参数的现有 Java 代码,我在 Kotlin 中尝试了以下代码:
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>>
那么正确的语法是什么?这有关系吗?