[swift-evolution] Pattern matching with Arrays

Lucas Neiva lneiva at me.com
Sun Dec 18 18:09:05 CST 2016


As a fellow functional programming aficionado, I'd like this very much, too. From lurking here, I think something like this is of low priority at the moment since it is and additive feature.

Especially head/tail matching looks very interesting. I also like the syntax you suggest here:
> case let [first, next...]:


> case let [first, last]:

The binding should be more like "[let first, let last]" though, to be more like the tuple matching. For the above also: "case [let head, let tail...]".


> On 18 Dec 2016, at 06:43, 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.
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list