[swift-evolution] Pattern matching with Arrays

Jay Abbott jay at abbott.me.uk
Mon Jan 16 23:53:32 CST 2017


Regarding the "some-day" list (or "deferred" proposals): It has been
discussed many times before, and Chris also agreed that it would be
useful[1], but nobody seems to have made it happen. So I have created a
trello board:
https://trello.com/b/dkVHvhGn/swift-deferred-proposals

Hopefully we can maintain it collectively, and it should be a handy quick
reference to previous discussions about specific subjects. I've added a few
lists/cards to get started, including a link to this discussion about
pattern matching. There's a README on the board that contains a link to
gain add/edit access to the board.

[1]
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160926/027353.html

On Fri, 23 Dec 2016 at 04:38 David Sweeris via swift-evolution <
swift-evolution at swift.org> wrote:

> On Dec 17, 2016, at 9:43 PM, Mathew Sanders via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> I've just joined the list (hi!) so not sure if this has been discussed in
> the past.
>
> Curious to hear if a future version of Swift might ever include some sort
> of pattern-matching extended to Arrays. Maybe something like the following:
>
> let numbers: [Int]
>
>
> switch numbers {
> case []:
>     // to match an empty array
>
>
> case [1, 2, 3]:
>     // to match array with specific values
>
>
> case [_, _, _]:
>     // to match array with 3 Ints
>
>
> case let [first, last]:
>     // match a 2 element array and bind first and last elements
>
>
> case let [first, next...]:
>     // to match array with at least one element
>     // bind first element, and bind other elements (could be empty)
>     // first: Int, next: [Int]
> }
>
> I love the pattern matching on tuples, and would love to see if extend to
> Arrays as well, but not sure if it fits with future goals for the language.
>
>
> +1
>
> Also, +1 for Erica’s idea of a “some day” list
>
> - Dave Sweeris
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170117/2ef63dba/attachment.html>


More information about the swift-evolution mailing list