Kotlin Reflection and Variance


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)
>>> 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'

What is happening here?


This was already discussed here:


@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?