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

Ayfri

12/07/2021, 9:22 AM
Hi, how can I get a list of
KMutableProperty1<*, *>
for the members of my class ? I only found
memberProperties
which returns a list of
KProperty<*, *>
and not castable so what should I use ?
r

Roukanken

12/13/2021, 8:10 PM
a simple
is KMutableProperty1<*, *>
check on the list should do it add a
filterIsInstance
which does the is check on whole collection and buala:
Copy code
data class Example(
    val one: Int,
    var two: String,
    val three: String,
    var four: Int,
)

val mutableProperties: List<KMutableProperty1<*, *>> =
    Example::class.memberProperties.filterIsInstance<KMutableProperty1<*, *>>()

mutableProperties.map { it.name to it.returnType }  
// == [(four, <http://kotlin.Int|kotlin.Int>), (two, kotlin.String)]
(the type of the variable is not needed, only there to be clear what it is)
a

Ayfri

12/13/2021, 8:15 PM
oh okay I see, thanks !