# Cannot add an element to a collection with += if it's declared as var

**URL:** <https://discuss.kotlinlang.org/t/cannot-add-an-element-to-a-collection-with-if-its-declared-as-var/7060>\
**Category:** Support\
**Created:** [March 15, 2018, 3:50pm UTC](https://discuss.kotlinlang.org/t/cannot-add-an-element-to-a-collection-with-if-its-declared-as-var/7060 "2018-03-15T15:50:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![reitzig](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/reitzig/32/6970_2.png) [@reitzig](https://discuss.kotlinlang.org/u/reitzig)\
**Post date:** [March 15, 2018, 3:50pm UTC](https://discuss.kotlinlang.org/t/cannot-add-an-element-to-a-collection-with-if-its-declared-as-var/7060/1 "2018-03-15T15:50:16Z")

</div>

Consider this completely innocent piece of code:

```kotlin
var set = mutableSetOf<String>()
set += "abd"

```

I get a compiler error:

> Error: Kotlin: Assignment operators ambiguity:  
> public operator fun Set.plus(element: String): Set defined in kotlin.collections  
> @InlineOnly public inline operator fun MutableCollection.plusAssign(element: String): Unit defined in kotlin.collections

This seems silly; how can `+` and `+=` ever be ambiguous? I clearly typed `+=`! Also, while using `+` _is_ legal in that position, it’s _not_ an assignment operator!

How do I make sense of this compiler error and/or language feature?

---

<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:** [March 15, 2018, 4:08pm UTC](https://discuss.kotlinlang.org/t/cannot-add-an-element-to-a-collection-with-if-its-declared-as-var/7060/2 "2018-03-15T16:08:56Z")

</div>

This expression is ambiguous because it can be interpreted in two ways:

- either you intended to create a copy of set with the new element `set = set + "abd"`
- or you wanted to mutate the existing set: `set.plusAssign("abd")`

This behavior of += operator is described here: [http://kotlinlang.org/docs/reference/operator-overloading.html#assignments](http://kotlinlang.org/docs/reference/operator-overloading.html#assignments)

---

<div class="post-metadata">

**Author:** ![reitzig](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/reitzig/32/6970_2.png) [@reitzig](https://discuss.kotlinlang.org/u/reitzig)\
**Post date:** [March 16, 2018, 7:33am UTC](https://discuss.kotlinlang.org/t/cannot-add-an-element-to-a-collection-with-if-its-declared-as-var/7060/3 "2018-03-16T07:33:51Z")

</div>

Uh. That is surprising.

Thanks!

I have a bad feeling about this. Those two sequences of statements have subtly different meanings:

```kotlin
val mset = mutableSetOf<String>()
mset += "abd"

var iset = setOf<String>()
iset += "abd"

```

If the type of a set is visually hidden (because it was inferred), I expect this to be error prone or at least a point of confusion. 😕
