Deep copy and partial changes


#1

Basically I like the idea how Kotlin does provide readonly / immutable classes
but I would like to know how elegant it’s possible to solve following problem:

Considering following:

data class Node(val title: String, val subNodes: List<Node> = emptyList())

now writing 

val root = Node("root  node", listOf(
    Node("child one"), Node("child two", listOf(
        Node("child two one"), Node("child two two")
    ))
 ))

 a) will "root.copy()" be a deep copy?
 b) how can I get a copy of the hierarchy with node "child two two" changed (adding a child node "child three")

 Recursion? Can the data class copy function be nicely interfaced?

Regards
Thomas


#2

No, copy is shallow. It’s just property assignments that are overridable.

What you want are commonly referred to as lenses (deep immutable object changes). Arrow has them.