Kotlin Reflection and Variance


#1

I’m trying to understand how variance works with reflection. As an example I’ve come across a seeming inconsistency in how variance is enforced. See this code -

>>> import kotlin.reflect.full.*
>>> data class One(val id: String)
>>> val data = One("Two")
>>> data.javaClass.kotlin.declaredMemberProperties.first().invoke(data)
Two
>>> data::class.declaredMemberProperties.first().invoke(data)
error: out-projected type 'KProperty1<out Line_28.One, Any?>' prohibits the use of 'public abstract fun invoke(p1: T): R defined in kotlin.reflect.KProperty1'
data::class.declaredMemberProperties.first().invoke(data)

What is happening here?


#2

This was already discussed here:


#3

@Wasabi375 Thanks very much for the reply and link reference! However my question still lingers, why does the invocation through .javaClass work, is there some kind of “variance erasure” happening?