> ## 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.

# Kotlin Addition Assignment

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:

```kotlin theme={"dark"}
// 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):**

```kotlin theme={"dark"}
class MutableCounter(var value: Int) {
    operator fun plusAssign(amount: Int) {
        this.value += amount
    }
}
```

**Implementing `plus` (Immutable Types):**

```kotlin theme={"dark"}
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`).

```kotlin theme={"dark"}
// 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 
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
