You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
}
Additional Detail from JIRA
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:
This is a generalization upon which we can build language support for Regexes, PEGs, etc. https://forums.swift.org/t/string-consumption/21907
The text was updated successfully, but these errors were encountered: