Is there a way to get from KProperty0 to KProperty1?

I cannot find a way to get from an instance-bound property to the respective class property, e.g. from ::myVal to MyClass::myVal.
I feel, that this should be possible since ::myVal is just a specialized case of MyClass::myVal.
Do I miss something?

1 Like

I am not an expert on Kotlin reflection.

But usually, it is very easy to get from a general case to a specialized case, but not easy to get from a specialized case to the general case.

If you have the means to determine the age of any person, it will be easy to determine the age of your uncle Herbert. But in the opposite direction, it is not true: Just knowing how to determine the age of your uncle Herbert does not give you the ability to determine everyone’s ages.

Your case has the same problem: KProperty0 may be the property of one specific object (this = “Uncle Herbert”), but it does not automatically contain a way to get this property of any other object of the same type.

The doc of KProperty0 even states that it can be a property that has its receiver bound to it (“Uncle Herbert”), but it can also be a property that is defined in a receiverless context. So, it is even possible that it is not “an int that is bound to some fixed object” - it could even be “just an int”. Therefore, in this case it is impossible to get a (meaningful) general case back from it.

1 Like

Ah, yes, you are of course right regarding the general situation. A KProperty0 might be something different from a member value, I didn’t think about it.
What I thought was, that ::myProp could (under the hood) be something like a KInstanceProperty which still knows its bound object instance. In this case it would be possible to go from instance to class and thus to the respective MyClass::myProp reference.
But as ::myProp just ends up being an ordinary KProperty0, the instance information is lost (or hidden).

So I fear there is no solution for my request, unless they introduce a special member property type.

1 Like