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

The `~=` (pattern matching) operator evaluates whether a specific value conforms to a defined pattern. It serves as the underlying boolean evaluation mechanism for Swift's pattern-matching constructs, implicitly driving the execution of `switch` statement `case` labels, `catch` clauses, `if case`, `guard case`, `for case`, and `while case` statements.

## Syntax and Operand Order

Unlike standard equality operators, the `~=` operator is asymmetrical. The order of operands is strictly defined and critical to its operation:

* **Left-Hand Side (LHS):** The pattern to match against.
* **Right-Hand Side (RHS):** The value being evaluated.

```swift theme={"dark"}
let isMatch = 1...5 ~= 3
print(isMatch) // Prints: true
```

When the Swift compiler evaluates a `switch` statement, it translates the `case` comparisons into `~=` expressions. For example, a `case 1...5:` evaluating a bound value of `3` is compiled under the hood as `1...5 ~= 3`.

## Standard Library Behavior

The Swift Standard Library provides default overloads for the `~=` operator across several common protocols and types:

* **`Equatable` Types:** If the pattern and the value are of the same type and conform to `Equatable`, `~=` delegates directly to the `==` operator.
* **Ranges (`Range`, `ClosedRange`):** When the pattern is a range and the value is of the range's underlying `Bound` type, `~=` evaluates to `pattern.contains(value)`.
* **Optionals:** Evaluates whether the unwrapped value of an optional matches the pattern. If the pattern itself is `nil` (e.g., `case nil:`), the operator evaluates to `true` when the value is `nil`. If the pattern is non-`nil` but the evaluated value is `nil`, it safely evaluates to `false`.
* **Errors:** Matches specific error types or domains within `catch` blocks.

## Custom Overloading

The `~=` operator can be globally overloaded to define custom pattern-matching logic, often between disparate types. To implement custom pattern matching, define a function for the `~=` operator that accepts the concrete pattern type as the first parameter and the concrete value type as the second parameter, returning a `Bool`.

*Warning: Never use unconstrained generic parameters (e.g., `<T, U>`) when overloading `~=`. Doing so creates a global catch-all that disables compile-time type checking for pattern matching across the entire module, allowing fundamentally incompatible types to compile in `switch` statements while silently failing at runtime.*

```swift theme={"dark"}
struct PrefixPattern {
    let prefix: String
}

struct TextValue {
    let content: String
}

// Overload the ~= operator for the custom types
func ~=(pattern: PrefixPattern, value: TextValue) -> Bool {
    return value.content.hasPrefix(pattern.prefix)
}

let pattern = PrefixPattern(prefix: "SYS_")
let value = TextValue(content: "SYS_BOOT_COMPLETE")

let customMatch = pattern ~= value
print(customMatch) // Prints: true
```

By defining this overload with concrete types, the custom evaluation logic is automatically injected into Swift's control flow structures whenever `PrefixPattern` is used as a `case` label against a `TextValue`.

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