# How to append a list to an immutable list of lists

**URL:** <https://discuss.kotlinlang.org/t/how-to-append-a-list-to-an-immutable-list-of-lists/38>\
**Category:** Uncategorized\
**Created:** [November 8, 2015, 9:18am UTC](https://discuss.kotlinlang.org/t/how-to-append-a-list-to-an-immutable-list-of-lists/38 "2015-11-08T09:18:30Z")\
**Posts on this page:** 9\
**Page:** 1

<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 8, 2015, 9:18am UTC](https://discuss.kotlinlang.org/t/how-to-append-a-list-to-an-immutable-list-of-lists/38/1 "2015-11-08T09:18:30Z")

</div>

Due to how "plus" is overloaded, I am unable to append a list to a list of lists. Code:

```kotlin
fun main(args: Array<String>) {
    // Added extra typing to demonstrate problem
    var someList: List<List<Int>> = listOf(emptyList())
    println("Size: " + someList.size)
    someList += emptyList<List<Int>>()
    println("Size: " + someList.size)
}
```

Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![hastebrot](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/hastebrot/32/1438_2.png) [@hastebrot](https://discuss.kotlinlang.org/u/hastebrot)\
**Post date:** [November 9, 2015, 3:38pm UTC](https://discuss.kotlinlang.org/t/how-to-append-a-list-to-an-immutable-list-of-lists/38/2 "2015-11-09T15:38:01Z")

</div>

As far as I can see `add()` and `plus()` are different methods. `plus()` is more like extending two collections (like `[].extend()` in Python or list concatenation in Scala with `++` or `:::`).

```kotlin
fun main(args: Array<String>) {
    var listA = listOf<List<Int>>(emptyList()).toArrayList()
    println(listA.size) // 1
    listA.add(emptyList())
    println(listA.size) // 2

    var listB = listOf<List<Int>>(emptyList())
    println(listB.size) // 1
    listB += listOf(emptyList())
    println(listB.size) // 2
}
```

Either "cast" to the MutableCollection interface (with `toArrayList()` or use `arrayListOf()` straight away) or extend the existing collection with another collection.

---

<div class="post-metadata">

**Author:** ![kenbarc](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/kenbarc/32/1693_2.png) [@kenbarc](https://discuss.kotlinlang.org/u/kenbarc)\
**Post date:** [November 9, 2015, 4:18pm UTC](https://discuss.kotlinlang.org/t/how-to-append-a-list-to-an-immutable-list-of-lists/38/3 "2015-11-09T16:18:27Z")

</div>

There appears to be two issues here. First **someList** is of type **List** , a read-only container and should not support the operator += ( **plusAssign** member function). To update a list you could use an **ArrayList** initialized with **arrayListOf**.

Second, the actual type arguments used in someList += emptyList\<List\<Int\>\>()  
should specify the element type of the empty list eg emptyList\<Int\>(). Here is some working code:

&nbsp;

```kotlin
import java.util.*
fun main(args: Array<String>) {
  // Added extra typing to demonstrate problem
    var someList: ArrayList<List<Int>> = arrayListOf(emptyList())
  println("Size: " + someList.size)
  someList.plusAssign(emptyList<Int>())
  //someList += emptyList<Int>()   // COMPILER ERROR: assignment operators ambiguity
    println("Size: " + someList.size)
  / ***** ORIGINAL CODE:
    // Added extra typing to demonstrate problem
    var someList: List<List<Int>> = listOf(emptyList())
    println("Size: " + someList.size)
    someList += emptyList<List<Int>>()
    println("Size: " + someList.size)
     ***** /
}
```

---

<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 9, 2015, 6:26pm UTC](https://discuss.kotlinlang.org/t/how-to-append-a-list-to-an-immutable-list-of-lists/38/4 "2015-11-09T18:26:55Z")

</div>

I don't want to upcast my type or use a concrete collection type or use a mutable list. I want to append two immutable lists. This is just a demonstration...what if I wanted my method to take a List\<List\<int\>\>? As an exercise, please fill in the contents of the function:

```kotlin
fun appendList<T>(listOfLists: List<List<T>>, toAppend: List<T>): List<List<T>> {
}
```

Of course both of the following work:

```kotlin
fun appendList<T>(listOfLists: List<List<T>>, toAppend: List<T>): List<List<T>> {
    // Use spread operator
    return listOf(*listOfLists.toTypedArray(), toAppend);
}

fun appendList<T>(listOfLists: List<List<T>>, toAppend: List<T>): List<List<T>> {
  // Turn to mutable list
  val ret = listOfLists.toArrayList()
  ret.add(toAppend)
  return ret
}
```

But this is confusing to users who might expect + to work. Due to type erasure, + will always have quirks like this. I had to add the following extension:

```kotlin
operator fun <T, C : Collection<T>> List<C>.plus(toAppend: C): List<C> {
    val ret = java.util.ArrayList<C>(this.size + 1)
    ret.addAll(this)
    ret.add(toAppend)
    return ret
}
```

This fixes the problem but I promise this will be confusing for users as they start to use Kotlin more and more.

---

<div class="post-metadata">

**Author:** ![hastebrot](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/hastebrot/32/1438_2.png) [@hastebrot](https://discuss.kotlinlang.org/u/hastebrot)\
**Post date:** [November 9, 2015, 10:58pm UTC](https://discuss.kotlinlang.org/t/how-to-append-a-list-to-an-immutable-list-of-lists/38/5 "2015-11-09T22:58:47Z")

</div>

1. Seems like Kotlin is falling back to `plus()` if no `plusAssign()` is implemented on the type. You are totally right, the compile shoud raise an error here.

1. It looked like the Kotlin plugin infers the type of `List&lt;Int&gt;`.

---

<div class="post-metadata">

**Author:** ![hastebrot](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/hastebrot/32/1438_2.png) [@hastebrot](https://discuss.kotlinlang.org/u/hastebrot)\
**Post date:** [November 9, 2015, 11:04pm UTC](https://discuss.kotlinlang.org/t/how-to-append-a-list-to-an-immutable-list-of-lists/38/6 "2015-11-09T23:04:37Z")

</div>

For me the `+` operator on a sequential type (String, List) concatenates it with another and returns a new object. So the LHS (left handside) and RHS of the operator should accept the same types.

\>Due to type erasure, + will always have quirks like this.

Isn’t this the case for both actions (append and extend/concatenate)?

---

<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 9, 2015, 11:19pm UTC](https://discuss.kotlinlang.org/t/how-to-append-a-list-to-an-immutable-list-of-lists/38/7 "2015-11-09T23:19:04Z")

</div>

Yes it is the case, it is unfair for me to mention type erasure here as it is unrelated to the problem.

“For me the `+` operator on a sequential type (String, List) concatenates it with another and returns a new object.” For me, + only adds a single item. If there needs to be a special case for + to concatenate two collections, it should be typed strictly enough that List\<List\<T\>\> + List\<T\> works as expected (or fails). This should be possible since overload resolution is performed statically.

---

<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 9, 2015, 11:26pm UTC](https://discuss.kotlinlang.org/t/how-to-append-a-list-to-an-immutable-list-of-lists/38/8 "2015-11-09T23:26:13Z")

</div>

For #1, I disagree. I like the fact that "foo += bar" falls back to "foo = foo + bar" in cases where plusAssign is not present (i.e. immutable collections). As is common for languages with immutable collections (e.g. Scala), people will prefer var with immutable collections over val with mutable ones except if really concerned about performance. I would rather not have to type "foo = foo + bar" constantly.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [November 12, 2015, 6:54pm UTC](https://discuss.kotlinlang.org/t/how-to-append-a-list-to-an-immutable-list-of-lists/38/9 "2015-11-12T18:54:32Z")

</div>

I agree, the result of overload resolution looks unexpected in this situation. I've opened an issue about it in our tracker: [https://youtrack.jetbrains.com/issue/KT-9992](https://youtrack.jetbrains.com/issue/KT-9992)
