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

A tuple pattern is a comma-separated list of zero or more patterns enclosed in parentheses, used to match and destructure the corresponding elements of a tuple value. It evaluates to `true` if, and only if, every sub-pattern within the parentheses successfully matches the corresponding element in the target tuple.

## Syntax

```swift theme={"dark"}
(pattern1, pattern2, ...)
```

## Mechanics and Behavior

**Arity and Type Matching**
The arity (number of elements) of the tuple pattern must exactly match the arity of the tuple being evaluated. The compiler enforces this strictly. Furthermore, the type of each sub-pattern must align with or be castable to the type of the corresponding tuple element.

```swift theme={"dark"}
let targetTuple = (1, "Swift")

// Matches: Arity is 2, types evaluate to Int and String
if case (1, "Swift") = targetTuple { 
    print("Exact match found.")
} 
```

**Sub-Pattern Composition**
A tuple pattern is a composite pattern. Its elements can be any valid Swift pattern, allowing for complex matching logic within a single structure. Common sub-patterns include:

* **Expression Patterns:** Matching exact literal values or expressions.
* **Wildcard Patterns (`_`):** Explicitly ignoring specific elements at a given index without binding them to memory.
* **Value-Binding Patterns (`let` / `var`):** Extracting elements into the local scope.

```swift theme={"dark"}
let compoundTuple = (404, "Not Found", false)

switch compoundTuple {
// Expression pattern + Wildcard patterns
case (404, _, _): 
    print("Matched HTTP 404.")
    
// Inline value-binding pattern + Wildcard + Expression pattern
// The explicit 'true' prevents this case from acting as an exhaustive catch-all
case (let code, _, true): 
    print("Resolved code: \(code)")
    
// Distributed value-binding (applies 'let' to all identifier sub-patterns)
// Acts as the exhaustive catch-all for the remaining tuple values
case let (code, message, isResolved): 
    print("Code: \(code), Message: \(message), Resolved: \(isResolved)")
}
```

**Type-Casting Patterns**
Tuple patterns support explicit type-casting patterns (`is` or `as`) to constrain matches dynamically. These type-casting patterns can be applied to individual elements within the tuple pattern, or to the entire tuple structure as a whole.

```swift theme={"dark"}
let mixedTuple: (Any, Any) = (10, "Swift")

// Type-casting patterns applied to individual elements
if case (let x as Int, let y as String) = mixedTuple { 
    print("Extracted Int: \(x), String: \(y)")
}

let anyTuple: Any = (10, 20)

// Type-casting pattern applied to the entire tuple structure
if case let (x, y) as (Int, Int) = anyTuple { 
    print("Extracted coordinates: \(x), \(y)")
}
```

**Nested Tuple Patterns**
Because tuple patterns accept any valid pattern as an element, they can be nested recursively to match multi-dimensional or hierarchical tuple structures. The compiler evaluates nested patterns depth-first.

```swift theme={"dark"}
let nestedTuple = (1, (2, 3))

// Matches the outer tuple and recursively destructures the inner tuple
if case (1, (let x, let y)) = nestedTuple { 
    print("Matched outer 1, extracted inner: \(x) and \(y)")
}
```

**Single-Element Tuples**
In Swift, a tuple with a single element is syntactically equivalent to the element itself. Therefore, a tuple pattern containing exactly one pattern is functionally identical to that single pattern without the enclosing parentheses.

```swift theme={"dark"}
let singleValue = 42

// The parentheses here are evaluated as grouping, not a strict tuple pattern
if case (42) = singleValue { 
    print("Matched single value.")
}
```

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