eygraber
08/01/2018, 6:22 AMthis::class.java
and javaClass
in regards to generics?
If I have this class defined:
class Test<T>(
val me: Class<T>
)
And I instantiate it like this, it's fine:
fun <T> T.test() {
Test(javaClass)
}
However, if I instantiate it like this, it won't compile:
fun <T> T.test() {
Test(this::class.java)
}
And the error is:
Type inference failed. Expected type mismatch:
Required: Test<T>
Found: Test<out T>
hho
08/01/2018, 6:34 AM.javaClass
is the equivalent of getClass()
in Java.
::class.java
is the equivalent of .class
in Java.
So what you're trying to do is this.class
– and that won't work, just as in Java.