> ## 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 Unary Plus

The `+` operator in Swift is a polymorphic, statically dispatched operator that functions primarily as a binary infix operator for arithmetic addition and collection concatenation, and secondarily as a unary prefix operator for returning the mathematical identity of an operand.

Because Swift enforces strict type safety, the `+` operator does not perform implicit type coercion. Both the left-hand side (`lhs`) and right-hand side (`rhs`) operands must evaluate to the exact same type, or a specific overload for the mixed types must exist in the compiler's resolution context.

## Operator Signatures

The operator is defined in the Swift Standard Library using the following fundamental signatures:

```swift theme={"dark"}
// Binary Infix
static func + (lhs: Self, rhs: Self) -> Self

// Unary Prefix
static prefix func + (x: Self) -> Self
```

## Protocol Bindings

The behavior of the `+` operator is dictated by the protocols to which the operand types conform:

* **`AdditiveArithmetic`**: For numeric types (e.g., `Int`, `Double`, `UInt`), the binary `+` operator satisfies the requirement of the `AdditiveArithmetic` protocol, executing standard mathematical addition. Furthermore, the unary prefix `+` is defined as an extension on `AdditiveArithmetic`. It returns the operand unmodified (`+x == x`) and is perfectly valid on both signed and unsigned integer types.
* **`RangeReplaceableCollection`**: For collections (e.g., `Array`, `String`), the `+` operator concatenates the `lhs` and `rhs` data structures, returning a newly allocated collection containing the combined elements. The `RangeReplaceableCollection` protocol makes no guarantees regarding underlying memory contiguity; whether the resulting collection allocates a contiguous block of memory or utilizes a non-contiguous structure (like a chunked deque or linked list) depends entirely on the specific conforming type's implementation.

## Safety and Overflow Mechanics

Unlike C or Objective-C, Swift's `+` operator includes mandatory, compiler-injected bounds checking for integer types.

If the evaluation of `lhs + rhs` results in a value that exceeds the maximum or minimum representable bounds of the underlying type, the operation triggers a runtime trap (a fatal error), preventing silent integer overflow. To explicitly permit two's-complement overflow wrapping, Swift provides the distinct `&+` (overflow addition) operator.

## Memory Semantics

The `+` operator is strictly non-mutating. It evaluates the operands and returns a newly initialized instance of the resulting type. It does not modify the memory state of either `lhs` or `rhs`. For in-place mutation, Swift utilizes the compound assignment operator (`+=`), which modifies the `lhs` directly and avoids the overhead of allocating a new instance when dealing with value semantics and copy-on-write (CoW) data structures.

## Custom Implementation (Overloading)

Developers can extend the `+` operator for custom structures or classes by defining a `static func` within the type definition or an extension.

```swift theme={"dark"}
struct Vector2D {
    var x: Double
    var y: Double
    
    static func + (lhs: Vector2D, rhs: Vector2D) -> Vector2D {
        return Vector2D(
            x: lhs.x + rhs.x, 
            y: lhs.y + rhs.y
        )
    }
}
```

When overloading, the operator inherits its precedence and associativity from the `AdditionPrecedence` group, which defines left-associativity and a lower precedence than `MultiplicationPrecedence`.

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