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, 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.
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:
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)O(m) time, where mm is the length of the RHS being appended (or amortized O(m)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)O(n + m) time complexity, where nn is the length of the LHS and mm 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.
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.
Master Swift with Deep Grasping Methodology!Learn More