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

# Swift Addition Assignment

The `+=` operator, formally known as the addition assignment operator, is a compound assignment operator that combines addition (or concatenation) and assignment into a single expression. It evaluates the right-hand side (RHS) expression, applies the addition operation against the current value of the left-hand side (LHS) operand, and assigns the resulting value back to the LHS operand.

```swift theme={"dark"}
lhs += rhs
```

## Return Type and Chaining

Unlike in languages such as C, C++, or Java, compound assignment operators in Swift evaluate to `Void` (the empty tuple `()`) rather than returning the mutated value. Consequently, assigning the result of a `+=` operation to a variable (e.g., `let x = a += 2`) is syntactically valid and will compile, but it assigns `()` to `x` and triggers a compiler warning about the inferred `Void` type.

Similarly, chaining the operator (e.g., `a += b += c`) is syntactically valid and parsed right-associatively as `a += (b += c)`. However, this expression fails during type checking. Because `b += c` evaluates to `Void`, the outer expression resolves to `a += ()`. The compiler rejects this because there is no `+=` operator overload that accepts `Void` as its right-hand side operand.

## Operand Requirements

* **LHS (Left-Hand Side):** Must be a mutable variable (declared with `var`) or a mutable property.
* **RHS (Right-Hand Side):** Must be an expression that evaluates to a type compatible with the LHS, typically of the exact same type unless an explicit overload exists for mixed types.

## Standard Library Implementation and `inout` Semantics

In the Swift Standard Library, `+=` is implemented as a static method. The LHS is passed using the `inout` keyword. Swift's `inout` parameters strictly utilize a "copy-in copy-out" (call by value result) semantic rather than strict pass-by-reference.

When the operator is invoked, the LHS value is copied in, mutated locally by the function, and then copied back out to the original location. While the compiler may optimize this to pass-by-reference for physical memory locations, the copy-in copy-out semantic ensures safe mutation for computed properties and properties with observers. For these properties, the value is retrieved via the getter, mutated, and written back via the setter, meaning there is no direct memory mutation.

For numeric types conforming to the `AdditiveArithmetic` protocol, the signature is defined as:

```swift theme={"dark"}
static func += (lhs: inout Self, rhs: Self)
```

## Type-Specific Behaviors

While the syntax remains uniform, the underlying operation dispatched by the compiler depends on the types of the operands:

1. **Numeric Types (Integers, Floating-point):** Performs standard arithmetic addition. Overflow behavior is trapped at runtime unless the masking addition assignment operator (`&+=`) is used.
2. **Strings:** Performs character sequence concatenation.
3. **Collections (Arrays, ContiguousArrays):** Appends the elements of the RHS sequence to the LHS collection. This is available for types conforming to `RangeReplaceableCollection`.

## Memory and Performance Semantics

For Swift's standard value types (such as `String` and `Array`), the `+=` operator interacts directly with Swift's Copy-on-Write (COW) optimization.

* If the LHS variable holds the sole reference to the underlying data buffer (uniquely referenced), `+=` mutates the buffer in place. For collections and strings, this operates in $O(m)$ time, where $m$ is the length of the RHS being appended (or amortized $O(m)$ if the buffer capacity needs to grow to accommodate the new elements).
* If the underlying buffer is shared with another variable, the `+=` operator forces a reallocation and copy of the existing buffer before performing the mutation. This results in $O(n + m)$ time complexity, where $n$ is the length of the LHS and $m$ is the length of the RHS.

## Operator Overloading

The `+=` operator can be overloaded for custom types. When defining a custom implementation, the LHS parameter must explicitly be marked as `inout`.

Swift fully supports defining operators as global functions outside of a type definition. However, declaring the operator as a `static func` within the type definition is the modern Swift convention and is strictly required when conforming to protocols like `AdditiveArithmetic`.

```swift theme={"dark"}
static func += (lhs: inout CustomType, rhs: CustomType) {
    lhs.internalProperty = lhs.internalProperty + rhs.internalProperty
}
```

By convention, if a custom type implements `+=`, it should also implement the standard addition operator (`+`) to ensure semantic consistency across the codebase.

<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 Swift 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>
