Viet Hoang
02/12/2019, 1:46 AM::class
and .javaClass.kotlin
? And why is that in below example, the later declaration works but the prior doesn’t ?
class Test {
fun testMethod() {
this::class.memberProperties.forEach { println(it.get(this)) } // compile error
this.javaClass.kotlin.memberProperties.forEach { println(it.get(this)) }
}
alex2069
02/12/2019, 1:58 AMthis::class
is KClass<out Test>
Whereas this.javaClass.kotlin
is KClass<Test>
val k1: KClass<out Test> = this::class
val m1: Collection<KProperty1<out Test, *>> = k1.memberProperties
val p1: KProperty1<out Test, *> = m1.first()
val k2: KClass<Test> = this.javaClass.kotlin
val m2: Collection<KProperty1<Test, *>> = k2.memberProperties
val p2: KProperty1<Test, *> = m2.first()
this::class
could be this class or its inheritorsthis::class
to be KClass<Test>