I've a question on `KClass.memberProperties` - the...
# reflect
m
I've a question on
KClass.memberProperties
- the docs say I get a
Collection<KProperty1<T, *>>
, but when I iterate this, the compiler infers
KProperty1<out T, Any?>
, which prevents me from calling
it.get(this)
on it. Anybody know why?
u
If you're calling
memberProperties
on a
::class
literal, then it's a known problem: https://youtrack.jetbrains.com/issue/KT-16432
The workaround is to cast
A::class
to
KClass<A>
manually :(
m
Yeah, I'm coming from
this::class
(which feels weird). Hmm, first time I see that
out
can be part of the type.
For the getter call,
it.getter.call(this)
does work. But both fail because I'm trying to access a private val. 😞
u
Note that even though you're getting the properties of this class, it could be a subclass at runtime, and invoking a subclass property passing the base class will fail. Of course it's not relevant for final classes at all, which is what we should probably do in KT-16432 to relax this restriction
To access a private val, you should make it accessible with
isAccessible = true
(which is subject to SecurityManager checks)
m
Yes, the class in question is a
data class
anyway