How does one iterate over the mutable properties o...
# announcements
n
How does one iterate over the mutable properties of a class? I can see that you can do
for (x in foo::class.memberProperties)
, but this only allows you to iterate over properties, i.e. no set function is available
n
besides not answering my question, this is a step in the wrong direction...
this iterates over all members, i.e. member functions, I'm only interested in properties
g
"but this only allows you to iterate over properties, i.e. no set function is available"
n
For those interested in the original question, a quick demo I was eventually able to figure out:
Copy code
for (x in p.memberProperties) {
        println("${x.name}: ${x.get(person)}")
        when (x.returnType) {
            String::class -> (x as KMutableProperty1<Person, String>).set(person, "hello")
            Int::class -> (x as KMutableProperty1<Person, Int>).set(person, 5)
        }
    }
@Gunslingor yes, the set function of the KProperty
How does one iterate over the mutable properties of a class
g
So member properties is a collection and you can iterate over it, looks like you are... so now your real question is "how do I check if a member property is mutable or static"?
i.e. you loop over them all and just do something on the mutable ones I guess. Curious what the application would be. Just trying to help
n
no
my real question is, how do I iterate over the mutable properties of a class 🙂
g
Okay, I eagerly await the answer... because I obviously don't understand the question.
n
what I posted above is the answer. One can add a simple check to ensure that it's really mutable
if (not (x is KMutableProperty)) { continue }
g
right, okay so you solved it... and it was the check I recommended, lol, after you show's me... no worries, we are talking different languages.
n
😕