Property reference

Playing with Reflection API and property reference I tried something like this :

data class Point(val x:Int, val y:Int)

 val p = Point(5,13)

   val getter1 = Point::x
   val getter2: (Point) -> Int  = Point::x
    println(getter1(p)) // 5
    println(getter2(p)) // 5

What I don’t understand is why with getter1 I was able to get reflection information about x for example I could do something like :

   println(getter1.visibility) // PUBLIC
   println(getter1.returnType) // kotlin.Int

while with getter2 I can’t, even if I use the @OptIn(ExperimentalReflectionOnLambdas::class)
with println(getter2.reflect()?.returnType) the result is null.

Can you explain to me the difference, please? :pray:

Yes, I also observe that reflect() is currently pretty limited and sometimes behaves strangely. The most funny part here is that the getter2 in your example actually is KProperty (Kotlin JVM/1.6.10) and you can just downcast it to get your reflection utils. So this is like reflect() can’t recognize that this is a reflection object already :wink:

One thing to note is that (Point) -> Int is a function type, so reflect() naturally returns KFunction, not KProperty. And KProperty is not a subtype of KFunction, so we can’t use KProperty as a result of reflect(). However, our (Point) -> Int above is clearly a getter for x, not the x property itself, so I don’t understand why reflect() doesn’t return KProperty.Getter in this case. Maybe there is some technical problem that still needs to be solved.


Yeah I see, thanks :grin:, and I don’t understand too why reflect() doesn’t work, as you said maybe there is some technical problem that still needs to be solved, Thank you for the explanation :pray: