I would like to return a copy of a class with a different value assigned to a val
property.
data class Person(val name: String, val age: Int)
fun main() {
val person = Person("Morné", 25)
val property = person::class.declaredMemberProperties.first { it.name == "age" }
person.copyWithValue(property.name, 22)
}
if age
was a var
then I could do it like follows:
fun main() {
val person = Person("Morné", 25)
val property = person::class.declaredMemberProperties.first { it.name == "age" }
if (property is KMutableProperty<*>)
property.setter.call(person, 22)
}