https://kotlinlang.org logo
#reflect
Title
# reflect
m

mbickel

04/19/2018, 10:53 AM
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

udalov

04/19/2018, 10:54 AM
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

mbickel

04/19/2018, 10:57 AM
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

udalov

04/19/2018, 11:06 AM
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

mbickel

04/19/2018, 11:30 AM
Yes, the class in question is a
data class
anyway