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)
}
If you really want to return just a copy of the object, you can use
copy
, e.g.:Otherwise if you really must edit the
age
it must not be aval
in the first place. Using reflection you could still adapt the value, but if the answers up to here already suffice, they are the way to go...For a more dynamic way you can use the following (I would still target the
copy
-method, as then you wouldn't accidently update the current object):which prints:
You could also use something like
.let { person::class.cast(it }
aftercallBy
if you want to continue with the actual type. If you only want this to work with thePerson
type you could also exchangeperson
withPerson
and cast it directly toas Person
.