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

An expression pattern represents the value of an expression and is evaluated by comparing that value against a control expression using the Swift standard library's pattern matching operator (`~=`). It acts as the underlying mechanism for evaluating `case` labels in `switch` statements, `catch` clauses, and `if case` / `guard case` constructs.

When the Swift compiler encounters an expression pattern, it implicitly translates the match attempt into a function call to the `~=` operator.

```swift theme={"dark"}
let controlValue = 5
let expressionPattern = 5

switch controlValue {
case expressionPattern:
    // Executes if (expressionPattern ~= controlValue) evaluates to true
    break
default:
    break
}
```

## The Pattern Matching Operator (`~=`)

The core of the expression pattern is the `~=` operator. The compiler requires this operator to return a `Bool` indicating whether the pattern successfully matches the value.

The function signature for the operator strictly dictates the order of operands:

1. **Left-Hand Side (LHS):** The pattern expression defined in the `case`.
2. **Right-Hand Side (RHS):** The control value being evaluated.

## Standard Library Implementations

Swift provides default overloads of the `~=` operator for common standard library types, defining their baseline expression pattern behavior:

* **`Equatable` Types:** If both the pattern and the value conform to `Equatable`, the `~=` operator delegates directly to the equality operator (`==`).

```swift theme={"dark"}
func ~=<T: Equatable>(a: T, b: T) -> Bool {
    return a == b
}
```

* **Ranges:** For types conforming to the `RangeExpression` protocol (such as `Range` and `ClosedRange`), the `~=` operator evaluates whether the control value is contained within the bounds of the pattern's range, utilizing the `contains(_:)` method.

```swift theme={"dark"}
func ~=<T: RangeExpression>(pattern: T, value: T.Bound) -> Bool {
    return pattern.contains(value)
}
```

## Operator Overloading for Custom Patterns

Because expression patterns are entirely dependent on the `~=` operator, the pattern matching system is extensible. Developers can define custom expression patterns by overloading the `~=` operator for specific type combinations.

This is achieved by defining a `static func` for the `~=` operator within a type (such as a `struct`, `class`, `enum`, or `extension`). Defining the operator as a static member prevents pollution of the global namespace while satisfying the compiler's requirements. The function must accept the desired pattern type as the first parameter and the target value type as the second parameter.

```swift theme={"dark"}
struct TargetValueType {}

struct CustomPatternType {
    // Defining a custom expression pattern mechanism
    static func ~= (pattern: CustomPatternType, value: TargetValueType) -> Bool {
        // Custom boolean evaluation logic
        return true
    }
}

// Compiler translation:
// case customPattern: -> CustomPatternType.~=(pattern: customPattern, value: targetValue)
```

By decoupling the pattern type from the value type, the expression pattern allows for asymmetric matching. The type of the expression in the `case` label does not need to match the type of the control value, provided a valid `~=` overload exists to bridge them.

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