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

diesieben07

08/13/2018, 9:05 PM
I am trying to obtain the value of every field in an object, but I am apparently unable to do so. I am doing this:
Copy code
fun <T : Any> getFieldValues(obj: T): List<Any?> {
    val cls = obj::class
    return cls.memberProperties.map { it.get(obj) }
}
But this does not compile, because I am getting
KClass<out T>
for
cls
. Is there any way to do this in a type-safe manner?
u

udalov

08/16/2018, 10:59 AM
No, it's a known problem: https://youtrack.jetbrains.com/issue/KT-16432 the only workaround we know is an unchecked cast 😨
Copy code
val cls = obj::class as KClass<Any>
d

diesieben07

08/16/2018, 11:01 AM
Thanks for the heads up!
2 Views