Compiler Error: Assignment operators ambiguity. All these functions match


#1

I used plusAssign operator function to add an item to a mutable list.

var attendances: MutableList<Attendance> = mutableListOf()

attendances += Attendance(request.newTime)

But it has a compiler error shown by IntelliJ IDEA which says:
Assignment operators ambiguity. All these functions match

It this a kotlin bug?

Note: If I change the var to val the error will be resolved.


#2

No, this is not a bug. Using the += operation with a mutable list has two possible interpretations - either to append an item to the existing list or to create a new list by appending the new value to the old list and to store the reference to the new list in the variable. Because the compiler can’t choose how to interpret this, it reports an ambiguity error.