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.
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
(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.
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.
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.
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.
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.
let singleValue = 42
// The parentheses here are evaluated as grouping, not a strict tuple pattern
if case (42) = singleValue {
print("Matched single value.")
}
Master Swift with Deep Grasping Methodology!Learn More