Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-10661] Generalized Destructuring Pattern Matching #53060

Open
milseman mannequin opened this issue May 10, 2019 · 0 comments
Open

[SR-10661] Generalized Destructuring Pattern Matching #53060

milseman mannequin opened this issue May 10, 2019 · 0 comments
Labels
compiler The Swift compiler in itself standard library Area: Standard library umbrella

Comments

@milseman
Copy link
Mannequin

milseman mannequin commented May 10, 2019

Previous ID SR-10661
Radar None
Original Reporter @milseman
Type Sub-task
Additional Detail from JIRA
Votes 0
Component/s Compiler, Standard Library
Labels Sub-task
Assignee None
Priority Medium

md5: fe4cea8be8f74cba5e03923434845d4b

Parent-Task:

Issue Description:

Add language-level support for generalized destructuring pattern matching. Currently, pattern matching through the ~= operator must return a Bool indicating success. This does not allow pattern matchers to provide results from pattern matching, such as in destructuring.

Early strawman:

protocol Pattern {
  associatedtype In
  associatedtype Out
  func match(_: In) -> Out?
}
...
let myValue: T = // ... something of type T
let pattern: MyTPattern<(U,U)> = // ... something that matches a T to (U, U)
let otherPattern: OtherTPattern<V> = // ... something that matches a T to V
switch myValue {
  case (let a: Int, let b: Int) <- pattern: // ... tries `pattern`, then tries default U->Int pattern
  case let pair <- pattern: // ... tries `pattern`, pair has type (U,U)
  case let d: Double <- otherPattern: // ... tries `otherPattern`, then tries default V->Double pattern
  case let num <- otherPattern: // ... tries `otherPattern`, num has type V
}

This is a generalization upon which we can build language support for Regexes, PEGs, etc. https://forums.swift.org/t/string-consumption/21907

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

0 participants