Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The += operator in Kotlin is an augmented assignment operator that combines addition (or element appending) with assignment. Rather than being a primitive built-in operation, Kotlin resolves a += b through operator overloading, translating the expression into either a mutating plusAssign function call or a non-mutating plus function call followed by reassignment.

Compiler Resolution Mechanics

When the Kotlin compiler encounters a += b, it performs a specific resolution sequence to determine the underlying bytecode:
  1. plusAssign Resolution: The compiler first checks if the receiver a has an accessible plusAssign member or extension function that accepts b. If it does, and the function returns Unit, the expression is translated to a.plusAssign(b). This mutates the object in place.
  2. plus Resolution: If plusAssign is not available, the compiler checks for a plus function. If a.plus(b) exists and returns a type compatible with a, the compiler translates the expression to a = a.plus(b). This requires a to be declared as a mutable variable (var).
  3. Ambiguity Error: If both plusAssign and plus are applicable (e.g., a is a var and its type implements both functions), the compiler throws an overload resolution ambiguity error to prevent unpredictable mutation semantics.

Syntax Translation

Depending on the resolved function, the compiler translates the syntax as follows:
// Scenario 1: Resolved to plusAssign (In-place mutation)
a += b
// Compiles to:
a.plusAssign(b)

// Scenario 2: Resolved to plus (Creation and reassignment)
a += b
// Compiles to:
a = a.plus(b)

Operator Overloading Implementation

To enable += for custom classes, you must define either plusAssign or plus using the operator keyword. Implementing plusAssign (Mutable Types):
class MutableCounter(var value: Int) {
    operator fun plusAssign(amount: Int) {
        this.value += amount
    }
}
Implementing plus (Immutable Types):
class ImmutableCounter(val value: Int) {
    operator fun plus(amount: Int): ImmutableCounter {
        return ImmutableCounter(this.value + amount)
    }
}

Collection Semantics

The += operator exhibits distinct behaviors when applied to Kotlin’s collection interfaces, heavily dependent on the collection’s mutability and the variable’s declaration (val vs. var):
  • val with MutableCollection: Resolves to plusAssign. The existing collection instance is mutated in place.
  • var with Read-Only Collection: Resolves to plus. A completely new collection instance is allocated, containing the original elements plus the new element, and the var reference is updated.
  • var with MutableCollection: Results in a compilation error. The compiler cannot safely determine whether to mutate the existing collection (plusAssign) or allocate a new one and reassign the reference (plus).
// Mutates the existing MutableList instance (plusAssign)
val mutableList = mutableListOf(1, 2)
mutableList += 3 

// Creates a new List instance and reassigns the reference (plus)
var readOnlyList = listOf(1, 2)
readOnlyList += 3 

// Compilation Error: Ambiguity between plusAssign and plus
var ambiguousList = mutableListOf(1, 2)
ambiguousList += 3 
Master Kotlin with Deep Grasping Methodology!Learn More