# Strange behaviour with collection plusAssign operator

**URL:** <https://discuss.kotlinlang.org/t/strange-behaviour-with-collection-plusassign-operator/21610>\
**Category:** Language Design\
**Created:** [April 22, 2021, 9:20am UTC](https://discuss.kotlinlang.org/t/strange-behaviour-with-collection-plusassign-operator/21610 "2021-04-22T09:20:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Balage-1551](https://avatars.discourse-cdn.com/v4/letter/b/3ec8ea/32.png) [@Balage-1551](https://discuss.kotlinlang.org/u/Balage-1551)\
**Post date:** [April 22, 2021, 9:20am UTC](https://discuss.kotlinlang.org/t/strange-behaviour-with-collection-plusassign-operator/21610/1 "2021-04-22T09:20:47Z")

</div>

I ran into a strange operator ambiguity when using `+=` on a mutable variable of collection (Set, List).

 ![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/7/789ab7706541ad56e027e49fb703f91a1c1f25d1.png)

When the variable is not `var`, it works correctly, because the compiler doesn’t want to release the assignment `c += 1` to an `=` and a `+`.

There are workarounds (using `val` or use the named call of the operator (`assignPlus`), but I think in this case the intention is clear and can’t recall a use case when one means the other way. So the clear precedency goes for the `plusAssign`.

---

<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:** [April 22, 2021, 10:30am UTC](https://discuss.kotlinlang.org/t/strange-behaviour-with-collection-plusassign-operator/21610/2 "2021-04-22T10:30:48Z")

</div>

> [@Balage-1551](#):
>
> but I think in this case the intention is clear

Is it? Do you want to call

```auto
c2 = c2 + 1

```

or

```auto
c2.add(1)

```

IMO both are equally implied by the `+=` syntax but 1 modifies the original list while the other creates a copy. This can easily lead to confusion and create bugs. There aleady was a discussion about this exact syntax here but I can’t find it right now, but from what I remember the consens was that `+=` isn’t really a good idea to use for lists or mutable lists. The first can easily lead to loads of unintentional copy operations turning a simple loop int o a O(n^2) operation while the other leads to ambiguity.
