# \[Pre-Pitch\] Pattern-Matching & Destructuring Projections

**URL:** <https://forums.swift.org/t/pre-pitch-pattern-matching-destructuring-projections/91299>\
**Category:** Pitches\
**Created:** [December 2, 2026, 6:26am UTC](https://forums.swift.org/t/pre-pitch-pattern-matching-destructuring-projections/91299 "2026-12-02T06:26:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ecs](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@ecs](https://forums.swift.org/u/ecs)\
**Post date:** [December 2, 2026, 6:26am UTC](https://forums.swift.org/t/pre-pitch-pattern-matching-destructuring-projections/91299/1 "2026-12-02T06:26:05Z")

</div>

There have been other struct/class/actor destructuring proposals, but I haven’t seen one like this pitched before. The idea is to allow projections that provide a tuple or enum view of the struct.

```swift
let rect = Rectangle(width: 10, height: 20)
// destructuring a projection (see “Proposed Solution” below)
let (w, h) = rect
let (width: w, height: h) = rect
let .area(totalArea) = rect

```

It looks pretty elegant and Swifty to me. I can’t find any edge cases that don’t already exist for enums.

* * *

**Introduction**

In Swift, enums and tuples enjoy rich, ergonomic pattern matching and destructuring (`switch`, `if case`, `guard case`, `let (...) =`, and `for (...) in`), while nominal types (`struct`, `class`, and `actor`) have no native way to expose destructurable shapes.

This pitch proposes allowing `struct`, `class`, and `actor` types to declare explicit **`case` projections** —lightweight, type-free lists of stored or computed property identifiers—that expose tuple and named-case views for pattern matching and destructuring.

* * *

**Motivation**

Today, destructuring or pattern-matching a `struct` or `class` requires either:

1. Manually converting it into a tuple before switching or iterating (`for (w, h) in rects.map { ($0.width, $0.height) }`), or
2. Writing boilerplate helper `enum` projections and switching on a computed property (`switch rect.shape`).

Previous discussions around automatic struct destructuring have stalled over legitimate concerns:

- **Encapsulation & Resilience:** Automatically exposing all stored properties as a positional tuple breaks call sites whenever a private property is added or reordered.
- **Syntax Collisions:** New destructuring delimiters (such as `{ a, b }` or `[a, b]`) conflict with closures, arrays, and dictionaries.
- **Single-Element Tuples:** Swift intentionally lacks single-element labeled tuples, making 1-property projections awkward.

By making destructuring an **explicit opt-in via `case` declarations** inside the type, we preserve encapsulation, introduce **zero new delimiters or operators** , and reuse 100% of Swift’s existing tuple and enum-case pattern grammar.

* * *

**Proposed Solution**

Allow `case` declarations inside `struct`, `class`, and `actor` bodies that reference existing stored or computed properties by name—without repeating type annotations.

```swift
struct Rectangle {
    var width: Double
    var height: Double

    // Computation remains on properties (preserving the Uniform Access Principle)
    var area: Double { width * height }
    var squareSide: Double? { width == height ? width : nil }

    // 1. Unnamed tuple projection (2+ properties) -> projects as (width: Double, height: Double)
    case (width, height)

    // 2. Named projections (1+ properties) -> projects as .caseName(...)
    case area(area)
    case square(squareSide)
}

```

**1. Pattern Matching (`switch`, `if case`, `guard case`)**

Because `case` is a compile-time property-getter projection rather than a `~=` Boolean predicate, all existing Swift pattern features (variable binding, optional unwrapping, wildcards, ranges, and nested patterns) work automatically:

```swift
switch rect {
case (0, 0):
    print("Degenerate point")

case (width: 10, height: let h):
    print("Fixed width 10, height \(h)")

case .square(let side?):
    print("Square with side \(side)")

case .area(0..<50):
    print("Small rectangle")

case let (w, h):
    print("General rectangle: \(w) x \(h)")
}

```

**2. Destructuring (`let` / `var` and `for-in`)**

Any context that already accepts tuple or enum-case patterns can now destructure nominal types directly:

```swift
// Local destructuring
let (w, h) = rect
let (width: w, height: h) = rect
let .area(totalArea) = rect

// Loop destructuring
for (w, h) in rectangles where w > h {
    print("Landscape: \(w) x \(h)")
}

// Nested destructuring
struct Frame {
    var origin: Point // declares `case (x, y)`
    var size: Size // declares `case (width, height)`
    case (origin, size)
}

let ((x, y), (w, h)) = frame

```

**3. Multiple Labeled Projections**

Types with multiple natural representations can expose multiple `case` projections, disambiguated by labels or case names just like overloaded functions:

```swift
struct Vector2D {
    var x: Double
    var y: Double
    var radius: Double { (x * x + y * y).squareRoot() }
    var angle: Double { atan2(y, x) }

    case (x, y)
    case (radius, angle)
}

switch vec {
case (x: 0, y: 0): ...
case (radius: 1, angle: let theta): ...
}

```

* * *

**Key Design Rules**

1. **No Types in `case` Declarations:**  
Property types are already declared on the stored/computed properties. Keeping `case` strictly as a list of property identifiers avoids repetition and allows refactoring between stored `let`/`var` and computed `var`without breaking call sites.
2. **No Bare Scalar Projections (`case area`):**  
Single-property projections must use a named case (`case area(area)`), while unnamed parentheses (`case (width, height)`) require 2 or more properties. This avoids Swift's single-element tuple restriction, prevents leading-dot `.member` lookup collisions with `enum` values, and keeps `case` patterns completely distinct from `~=` expression patterns.
3. **Compile-Time Accessor Semantics (Not `~=` Sugar):**  
`case` projects property getters rather than calling a runtime `~=` predicate. Wildcarded fields (`let (w, _) = rect`) do not need to evaluate unused computed property getters.
4. **Uniform Across Value and Reference Types:**  
Because `case` only reads properties, it works identically on `struct`, `class` (respecting inheritance and overridden computed properties), and `actor` (following standard actor-isolation rules).

---

<div class="post-metadata">

**Author:** ![ecs](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@ecs](https://forums.swift.org/u/ecs)\
**Post date:** [December 2, 2026, 7:05am UTC](https://forums.swift.org/t/pre-pitch-pattern-matching-destructuring-projections/91299/2 "2026-12-02T07:05:46Z")

</div>

A future direction that occurred to me is composing multiple projections together.

* * *

**1. What Works Already**

In Swift’s existing grammar, `if case` and `guard case` already accept **multiple comma-separated pattern-matching clauses** against the same subject—and `for-in` supports `where` clauses:

```swift
// Matching two projections of `rect` in a single `if case`:
if case (width: let w, height: let h) = rect,
   case .area(100) = rect {
    print("Area is 100 with dimensions \(w) x \(h)")
}

// Or binding values from BOTH projections at once:
if case .polar(let r, let theta) = vec,
   case (x: let x, y: let y) = vec,
   r > 10 {
    print("Polar (\(r), \(theta)) -> Cartesian (\(x), \(y))")
}

```

**2. True Pattern Composition: The `&` Pattern Operator**

Conjunction Patterns are especially interesting inside `switch` arms and `for case` loop headers where comma-chaining isn't available: (`pattern1 & pattern2`)

Because `&` is not currently a valid operator inside a Swift **pattern** context (bitwise `&` only exists in expression contexts), `pattern1 & pattern2` is completely unambiguous:

```swift
// 1. Inside a `switch` arm (impossible with comma-chaining!):
switch rect {
case (let w, let h) & .area(100):
    print("Dimensions \(w)x\(h) with exact area 100")

case .square(let side?) & .area(let a):
    print("Square of side \(side) and area \(a)")

case (width: let w, height: _) & (radius: let r, angle: _):
    print("Mixed projections: width \(w), radius \(r)")
}

// 2. Inside a `for-in` loop header:
for (let w, let h) & .area(0..<100) in rectangles {
    print("Small rectangle: \(w) x \(h)")
}

// 3. Inside `guard case`:
guard case (let w, let h) & .square(let side?) = rect else { return }

```
