Skip to main content
An identifier pattern matches any value and binds that evaluated value to a variable or constant name. It is the most fundamental pattern in Swift’s pattern matching engine, acting as an unconditional match that captures the assigned value into a newly declared identifier in the current scope. In Swift’s grammar, an identifier pattern is simply a valid identifier name. It is almost always utilized as a sub-pattern within a value-binding pattern (using let or var), which dictates the mutability of the bound identifier.

Mechanics

  1. Unconditional Matching: The identifier pattern does not evaluate the structure, type, or state of the value for equality. It inherently succeeds and accepts whatever value is passed to it.
  2. Binding: Upon a successful match, the Swift compiler allocates the identifier in the local scope and binds the matched value to it.
  3. Composition: Identifier patterns are frequently nested within structural patterns (such as tuple patterns or enumeration case patterns) to extract and bind specific sub-values.

Grammar and Resolution Context

The Swift compiler resolves an identifier based on its surrounding lexical context. An identifier only functions as an identifier pattern when it is explicitly introducing a new binding. If an identifier is used within a pattern-matching context (like a switch statement’s case) without a value-binding keyword, the compiler resolves it as an expression pattern (evaluating against an existing variable in scope) rather than an identifier pattern.
Tired of Poor Swift Skills? Fix That With Deep Grasping!Learn More