# Deep copy and partial changes

**URL:** <https://discuss.kotlinlang.org/t/deep-copy-and-partial-changes/10529>\
**Category:** Language Design\
**Created:** [November 26, 2018, 5:51pm UTC](https://discuss.kotlinlang.org/t/deep-copy-and-partial-changes/10529 "2018-11-26T17:51:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomas.lehmann.private](https://avatars.discourse-cdn.com/v4/letter/t/bc8723/32.png) [@thomas.lehmann.private](https://discuss.kotlinlang.org/u/thomas.lehmann.private)\
**Post date:** [November 26, 2018, 5:51pm UTC](https://discuss.kotlinlang.org/t/deep-copy-and-partial-changes/10529/1 "2018-11-26T17:51:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cretz](https://avatars.discourse-cdn.com/v4/letter/c/d2c977/32.png) [@cretz](https://discuss.kotlinlang.org/u/cretz)\
**Post date:** [November 26, 2018, 9:24pm UTC](https://discuss.kotlinlang.org/t/deep-copy-and-partial-changes/10529/2 "2018-11-26T21:24:04Z")

</div>

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](http://kotlinlang.org/docs/reference/data-classes.html#copying).
