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.
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:
- Manually converting it into a tuple before switching or iterating (
for (w, h) in rects.map { ($0.width, $0.height) }), or - Writing boilerplate helper
enumprojections 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.
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:
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:
// 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:
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
- No Types in
caseDeclarations:
Property types are already declared on the stored/computed properties. Keepingcasestrictly as a list of property identifiers avoids repetition and allows refactoring between storedlet/varand computedvarwithout breaking call sites. - 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.memberlookup collisions withenumvalues, and keepscasepatterns completely distinct from~=expression patterns. - Compile-Time Accessor Semantics (Not
~=Sugar):
caseprojects property getters rather than calling a runtime~=predicate. Wildcarded fields (let (w, _) = rect) do not need to evaluate unused computed property getters. - Uniform Across Value and Reference Types:
Becausecaseonly reads properties, it works identically onstruct,class(respecting inheritance and overridden computed properties), andactor(following standard actor-isolation rules).