Why won't this compile? ``` fun String.differingCh...
# getting-started
b
Why won't this compile?
Copy code
fun String.differingCharacters(other: String): List<Char> {
    var endList: MutableList<Char> = mutableListOf()
    (0..min(this.length, other.length)).forEach { index ->
        val thisChar = this[index]
        val otherChar = other[index]
        if (thisChar != otherChar) {
            endList += thisChar
        }
    }
    if (this.length < other.length) {
        endList += other.substring(this.length).toCharArray().asList()
    } else if (other.length > this.length) {
        endList += other.substring(this.length).toCharArray().asList()
    }
    return endList
}
Errors:
Error:(123, 21) Kotlin: Assignment operators ambiguity:
public operator fun <T> Collection<Char>.plus(element: Char): List<Char> defined in kotlin.collections
@InlineOnly public operator inline fun <T> MutableCollection<in Char>.plusAssign(element: Char): Unit defined in kotlin.collections
Error:(127, 17) Kotlin: Assignment operators ambiguity:
public operator fun <T> Collection<Char>.plus(elements: Iterable<Char>): List<Char> defined in kotlin.collections
@InlineOnly public operator inline fun <T> MutableCollection<in Char>.plusAssign(elements: Iterable<Char>): Unit defined in kotlin.collections
Error:(129, 17) Kotlin: Assignment operators ambiguity:
public operator fun <T> Collection<Char>.plus(elements: Iterable<Char>): List<Char> defined in kotlin.collections
@InlineOnly public operator inline fun <T> MutableCollection<in Char>.plusAssign(elements: Iterable<Char>): Unit defined in kotlin.collections
i
+=
operator on var MutableList can be interpreted either as
list.plusAssign(char)
or as
list = list.plus(char)
, therefore it's an ambiguity
So either make the list read-only or declare variable as val
b
that's... wow, really? So if
plusAssign
wasn't there what would happen?