Transforming data-classes with a function


I often find myself looking for something to make copying of data-objects easier.

data class User(val name: String, val age: Int)

val updated = user.copy (age = age + 1)
val updated = user.copy {age -> age +1}
val updated = user.copy {it + 1}
val updated = user.copy {(_,_,age,_,_) -> age + 1}

Maybe if copy could accept a runnable with receiver. Or additional componentFunctions updateAge etc. Or additional constructor taking 1 argument each.

Especially for nested data-classes something like this would be cool:

data class User(val name: String, val lastLogin: Int)
data class Permission(val user: User)

perm = perm.update {
    user.update {
        lastLogin = now()


perm = perm.update {
    user.lastLogin = now()

As you can see I’m not sure what I need exactly, just something to shorten nested statements like this:

perm = perm.copy(user = perm.user.copy(lastLogin = perm.user.lastLogin + 2)

Regards and thanks for your awesome work!


A somewhat related topic has been discussed here: Tuple Interfaces (for data classes)


Sounds interesting. I think what would help me a lot would be just an update function for a data class that generates a builder and returns the copied objects.

copy = perm.update {
    type = "test"
    user = user.update {
        lastLogin = now()