<div dir="ltr"><div class="markdown-here-wrapper" style=""><p style="margin:0px 0px 1.2em!important">You can process lists using pattern matching today:</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code class="hljs language-swift" style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248);white-space:pre;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important;display:block;overflow-x:auto;padding:0.5em;color:rgb(51,51,51);background:rgb(248,248,248)"><span class="hljs-class"><span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">enum</span> <span class="hljs-title" style="color:rgb(153,0,0);font-weight:bold;color:rgb(68,85,136);font-weight:bold">List</span><<span class="hljs-title" style="color:rgb(153,0,0);font-weight:bold;color:rgb(68,85,136);font-weight:bold">T</span>> </span>{
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">case</span> <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Nil</span>
indirect <span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">case</span> <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Cons</span>(<span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">T</span>, <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">List</span><<span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">T</span>>)
}
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> list: <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">List</span><<span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Int</span>> = .<span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Cons</span>(<span class="hljs-number" style="color:rgb(0,128,128)">2</span>, .<span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Cons</span>(<span class="hljs-number" style="color:rgb(0,128,128)">3</span>, .<span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Nil</span>))
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">switch</span> list {
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">case</span> .<span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Nil</span>: ...
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">case</span> .<span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Cons</span>(<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> x, .<span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Nil</span>): ...
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">case</span> .<span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Cons</span>(<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> x, .<span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Cons</span>(<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> y, .<span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Nil</span>)): ...
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">default</span>: handleLongList(list)
}
</code></pre>
<p style="margin:0px 0px 1.2em!important">The thing is, such lists are not idiomatic in Swift. Pattern matching on <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">Array</code> also kind-of works:</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code class="hljs language-swift" style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248);white-space:pre;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important;display:block;overflow-x:auto;padding:0.5em;color:rgb(51,51,51);background:rgb(248,248,248)"><span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> array: <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Array</span><<span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Int</span>> = ...
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">switch</span> array.first {
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">case</span> .some(<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> first):
processHead(first)
processTail(array.<span class="hljs-built_in" style="color:rgb(0,134,179)">dropFirst</span>())
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">case</span> .<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">none</span>:
handleNil()
}
</code></pre>
<p style="margin:0px 0px 1.2em!important">Again, it’s not the most idiomatic way to do this in Swift. Also, there is no guaranteed tail-call optimization.</p>
<p style="margin:0px 0px 1.2em!important">2016-09-06 17:48 GMT+03:00 Jean-Denis Muys via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>>:</p>
<p style="margin:0px 0px 1.2em!important"></p><div class="markdown-here-exclude"><p></p><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Hello,<div><br></div><div>I would like to suggest an additive evolution to Swift that might be in scope of phase 1 of Swift 4 (because it might have an impact on the ABI).</div><div><br></div><div>The idea is to extend the pattern matching abilities of Swift to enable a recursive programming style that’s very common in languages such as Lisp, ML, or Prolog on a collection that is processed as a list. By analogy to ML, Swift could do that on tuples, or on arrays, or on any similar, perhaps new, data type. This would allow the following for example:</div></div></blockquote><p></p></div><p style="margin:0px 0px 1.2em!important"></p>
<div title="MDH:WW91IGNhbiBwcm9jZXNzIGxpc3RzIHVzaW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