# Deep copy of list should be simplified

**URL:** <https://discuss.kotlinlang.org/t/deep-copy-of-list-should-be-simplified/15458>\
**Category:** Language Design\
**Created:** [December 16, 2019, 4:32pm UTC](https://discuss.kotlinlang.org/t/deep-copy-of-list-should-be-simplified/15458 "2019-12-16T16:32:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SteadyDisorder](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@SteadyDisorder](https://discuss.kotlinlang.org/u/SteadyDisorder)\
**Post date:** [December 16, 2019, 4:32pm UTC](https://discuss.kotlinlang.org/t/deep-copy-of-list-should-be-simplified/15458/1 "2019-12-16T16:32:33Z")

</div>

As for today, I’ve failed to found a simple, universal solution for creating deep copies of classes that dereive from Iterable. Currently, creating a simple copy of Kotlin collection looks like this:

```
val newList = listOf()
for element in oldList {
    newList.add(element.copy())
}
return newList

```

This should be simplified, to a single method, the way that Rust / Python handles is, for example

```
val newList = oldList.clone()

```

Creating an extenstion function doesn’t seem to be possible, since `.copy()` method is only provided for some classes.

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [December 16, 2019, 4:53pm UTC](https://discuss.kotlinlang.org/t/deep-copy-of-list-should-be-simplified/15458/2 "2019-12-16T16:53:08Z")

</div>

```kotlin
val newList = oldList.map { it.copy() }

```

---

<div class="post-metadata">

**Author:** ![SteadyDisorder](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@SteadyDisorder](https://discuss.kotlinlang.org/u/SteadyDisorder)\
**Post date:** [December 16, 2019, 5:04pm UTC](https://discuss.kotlinlang.org/t/deep-copy-of-list-should-be-simplified/15458/3 "2019-12-16T17:04:35Z")

</div>

I’m aware of this, but still, a simple `clone()` or `copy()` would be much more pleasant to use

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [December 16, 2019, 5:20pm UTC](https://discuss.kotlinlang.org/t/deep-copy-of-list-should-be-simplified/15458/4 "2019-12-16T17:20:28Z")

</div>

You can write your own with one line of code. The problem with a standard function is that there is no commonly agreed definition of deep. How deep? One level deep, x levels, all levels? What about lists of lists of list? What about recursive data structures, where the innermost values are equal to the outermost values?

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [December 16, 2019, 5:20pm UTC](https://discuss.kotlinlang.org/t/deep-copy-of-list-should-be-simplified/15458/5 "2019-12-16T17:20:54Z")

</div>

I’m not sure I understand what exactly are you asking, how would this `.copy()` behave if the objects in the list don’t have a `.copy()` method?

---

<div class="post-metadata">

**Author:** ![andob](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@andob](https://discuss.kotlinlang.org/u/andob)\
**Post date:** [December 16, 2019, 5:24pm UTC](https://discuss.kotlinlang.org/t/deep-copy-of-list-should-be-simplified/15458/6 "2019-12-16T17:24:03Z")

</div>

There are many ways to clone objects. For instance, without even implementing the Cloneable interface, but assuming your objects are serializable, you could:

```auto
fun <T> T.clone() : T
{
    val byteArrayOutputStream=ByteArrayOutputStream()
    ObjectOutputStream(byteArrayOutputStream).use { outputStream ->
        outputStream.writeObject(this)
    }

    val bytes=byteArrayOutputStream.toByteArray()

    ObjectInputStream(ByteArrayInputStream(bytes)).use { inputStream ->
        return inputStream.readObject() as T
    }
}

```

Usage:

```auto
val list2 = list1.clone()
val anyOtherObject2 = anyOtherObject1.clone()

```

But this method does not gives the programmer the flexibility to choose how to clone, what to clone etc

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [December 16, 2019, 9:28pm UTC](https://discuss.kotlinlang.org/t/deep-copy-of-list-should-be-simplified/15458/7 "2019-12-16T21:28:17Z")

</div>

But this is not an assumption that can be made by the standard library. There are many reasons why people don’t like java `Object.clone`. Even the documentation of it states that you can’t make any assumptions about it’s result. Not even simple statements like `x.clone() == x` or `x.clone().getClass == x.getClass()` can be assumed to always be true. All that is supposed to be guaranteed is that `x.clone() !== x`, which means the function is practically useless for any generic usage. Thankfully kotlin didn’t make the same mistake and removed this function form `Any`.

There might be a point in discussing adding a `Cloneable` interface to kotlin and deciding on a strict definitions what this interface should mean. Otherwise there is no way to create a `deepCopy`. The problem I see with this is that the best definition of `Cloneable` probably depends on the project and therefor can not be made by the standard library.

---

<div class="post-metadata">

**Author:** ![igor\_ganapolsky](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/igor_ganapolsky/32/5705_2.png) [@igor\_ganapolsky](https://discuss.kotlinlang.org/u/igor_ganapolsky)\
**Post date:** [February 17, 2021, 3:52pm UTC](https://discuss.kotlinlang.org/t/deep-copy-of-list-should-be-simplified/15458/8 "2021-02-17T15:52:32Z")

</div>

Were you looking to clone mutable state?
