[swift-evolution] Pattern matching with Arrays

Joe Groff jgroff at apple.com
Tue Jan 3 12:10:22 CST 2017

> On Dec 22, 2016, at 7:43 PM, Robert Widmann <devteam.codafi at gmail.com> wrote:
> Do you think there’s room for a more general Pattern Synonyms-like <https://ghc.haskell.org/trac/ghc/wiki/PatternSynonyms> feature that could extend this to things that look tuple-y?  We had a short conversation on Twitter 'round about the release of Swift 1.2 about Swiftz’s HList <https://github.com/typelift/Swiftz/blob/master/Sources/HList.swift#L185> implementation and my desire to be able to destructure them into tuples for native pattern matching.

My personal favorite design for user-extensible pattern syntax is F#'s "active pattern" feature, which lets you declare a set of mutual exclusive, total or partial conditions along with a function body that computes which condition holds. For the specific case of container patterns, though, I feel like it's worth keeping a close association with Collection semantics, so that:

- [] matches when the incoming collection's startIndex == endIndex,
- [<pattern>, <pattern-list>] fails if startIndex == endIndex, or else matches collection[startIndex] against <pattern> and recursively matches collection[startIndex.advancedBy(1)..<endIndex] against [<pattern-list>]
- [<pattern>...] matches the remaining collection against <pattern>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170103/aae95db2/attachment.html>

More information about the swift-evolution mailing list