Trying to call existing Java code that expects a Class
as a parameter, I tried code along the lines of this in 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
}
But that does not compile with the following error:
Type inference failed. Expected type mismatch: inferred type is Class<KotlinCode> but Class<JavaCode<String>> was expected
So I tried to force a cast (as suggested in this answer):
hello(GenericArgument::class.java as Class<Callable<String>>)
But that has a warning:
Unchecked cast: Class<KotlinCode> to Class<JavaCode<String>>
So what is the correct syntax to use? Is this related?