Skip to main content

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.

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.
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 (==).
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.
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.
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.
Master Swift with Deep Grasping Methodology!Learn More