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

The `+` operator in Swift is a statically dispatched, strongly typed binary infix operator primarily used for arithmetic addition and collection concatenation. It also functions as a unary prefix operator that returns the unmodified value of its operand. While default arithmetic implementations typically require the left-hand side (`lhs`) and right-hand side (`rhs`) operands to share the same type, Swift's operator overloading fully supports mixed-type implementations. The standard library and Foundation frequently utilize mixed-type `+` operators, such as adding a `Substring` to a `String`, or an `Int` to an `UnsafePointer<T>`.

## Protocol Requirements and Implementation

Under the hood, the `+` operator is not a hardcoded compiler primitive. For numeric types (e.g., `Int`, `Double`, `Float`), it is defined as a static method requirement within the `AdditiveArithmetic` standard library protocol:

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

For non-arithmetic types, such as strings and collections, `+` is not a protocol requirement. Instead, concatenation behavior is provided via generic global functions or protocol extensions constrained to specific types (like `RangeReplaceableCollection` or `StringProtocol`).

## Precedence and Associativity

The binary `+` operator belongs to the `AdditionPrecedence` group.

* **Associativity**: Left-associative. When multiple `+` operators appear in sequence, they are evaluated from left to right.
* **Precedence**: It evaluates after `MultiplicationPrecedence` operators (like `*` and `/`) and before `AssignmentPrecedence` operators (like the compound assignment operators `+=` or `-=`). Note that the basic assignment operator (`=`) is a special language token and does not belong to a precedence group.

## Overflow Behavior

By default, Swift's arithmetic `+` operator performs overflow checking. If the evaluation of the addition exceeds the maximum or minimum representable bounds of the underlying type, the operation will trigger a runtime trap (a fatal error), preventing undefined behavior.

To perform two's-complement wrapping addition without trapping, Swift provides the distinct overflow addition operator (`&+`):

```swift theme={"dark"}
// Using a variable to bypass the compiler's static constant evaluator
var a: UInt8 = 255

// Standard addition (Traps at runtime due to overflow)
// let b = a + 1 

// Wrapping addition (Returns 0)
let c = a &+ 1 
```

## Operator Overloading

Developers can overload the `+` operator for custom types. Swift language rules require that operators declared within a type be defined as `static` methods. This ensures the operator does not take an implicit `self` instance parameter, maintaining symmetry between the left and right operands for binary operators and matching the signature of global operator functions.

If a custom type conforms to `AdditiveArithmetic`, implementing this operator (along with `-` and `zero`) fulfills the protocol's core requirements. Developers can also define mixed-type overloads by specifying different types for the `lhs` and `rhs` parameters.

```swift theme={"dark"}
struct Vector {
    var x: Int
    var y: Int

    static func + (lhs: Vector, rhs: Vector) -> Vector {
        return Vector(x: lhs.x + rhs.x, y: lhs.y + rhs.y)
    }
}
```

## Unary Prefix Form

The `+` operator is also defined as a prefix operator. For types conforming to `AdditiveArithmetic`—which includes both signed and unsigned numeric types (e.g., `Int`, `UInt8`)—it acts as an identity operation, returning the value unchanged. It exists primarily for syntactic symmetry with the unary minus (`-`) operator. Like all operators defined within a type or protocol, it must be declared as a `static` method:

```swift theme={"dark"}
static prefix func + (x: Self) -> Self
```

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