<div dir="ltr">+1 <div>I have wanted this since the first beta. I hadn&#39;t proposed because I haven&#39;t come up with a nice syntax to do this in functions/methods. I don&#39;t particularly like</div><div><div style="font-size:13px"><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">    func</span><span style="font-family:Menlo;font-size:11px"> takesATuple(someInt: </span><span style="font-family:Menlo;font-size:11px;color:rgb(112,61,170)">Int</span><span style="font-family:Menlo;font-size:11px">, tuple (valueA, valueB): (<span style="color:rgb(112,61,170)">String</span>, <span style="color:rgb(112,61,170)">String</span>))</span></div></div><div><font face="Menlo"><span style="font-size:11px"><br></span></font></div>and the closes that I have come is to simply reuse the closure syntax with<div><br></div><div><div style="font-size:13px"><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">    func</span><span style="font-family:Menlo;font-size:11px"> takesATuple(someInt: </span><span style="font-family:Menlo;font-size:11px;color:rgb(112,61,170)">Int</span><span style="font-family:Menlo;font-size:11px">, tuple: (<span style="color:rgb(112,61,170)">String</span>, <span style="color:rgb(112,61,170)">String</span>)) { </span><span style="font-family:Menlo;font-size:11px"> (someInt, </span><span style="font-family:Menlo;font-size:11px">(valueA, valueB)) in</span></div></div><div style="font-size:13px"><span style="font-family:Menlo;font-size:11px"><br></span></div><div><font face="Menlo"><span style="font-size:11px">but that gets confusing in my opinion, specifically if you choose to have different names inside and outside.</span></font></div><div><span style="font-family:Menlo;font-size:11px"><br></span></div><div><span style="font-family:Menlo;font-size:11px"><br></span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 5, 2016 at 11:22 AM, Dennis Weissmann via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Following a short discussion with positive feedback on [swift-users](<a href="http://thread.gmane.org/gmane.comp.lang.swift.user/1812" target="_blank">http://thread.gmane.org/gmane.comp.lang.swift.user/1812</a>) I’d like to discuss the following:</div><div><br></div><div>Tuples should be destructible into their components in parameter lists.</div><div><br></div><div>Consider the following code:</div><div><br></div><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span><span> a = [</span><span style="color:rgb(39,42,216)">0</span><span>,</span><span style="color:rgb(39,42,216)">1</span><span>,</span><span style="color:rgb(39,42,216)">2</span><span>,</span><span style="color:rgb(39,42,216)">3</span><span>,</span><span style="color:rgb(39,42,216)">4</span><span>,</span><span style="color:rgb(39,42,216)">5</span><span>,</span><span style="color:rgb(39,42,216)">6</span><span>,</span><span style="color:rgb(39,42,216)">7</span><span>,</span><span style="color:rgb(39,42,216)">8</span><span>,</span><span style="color:rgb(39,42,216)">9</span><span>]</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span><span> b = [</span><span style="color:rgb(39,42,216)">0</span><span>,</span><span style="color:rgb(39,42,216)">1</span><span>,</span><span style="color:rgb(39,42,216)">2</span><span>,</span><span style="color:rgb(39,42,216)">3</span><span>,</span><span style="color:rgb(39,42,216)">4</span><span>,</span><span style="color:rgb(39,42,216)">5</span><span>,</span><span style="color:rgb(39,42,216)">6</span><span>,</span><span style="color:rgb(39,42,216)">7</span><span>,</span><span style="color:rgb(39,42,216)">8</span><span>,</span><span style="color:rgb(39,42,216)">9</span><span>]</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span><span> c = </span><span style="color:rgb(61,29,129)">zip</span><span>(</span><span style="color:rgb(79,129,135)">a</span><span>,</span><span style="color:rgb(79,129,135)">b</span><span>).</span><span style="color:rgb(61,29,129)">reduce</span><span>(</span><span style="color:rgb(39,42,216)">0</span><span>) { acc, tuple</span><span> </span><span style="color:rgb(187,44,162)">in</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>  acc + tuple.</span><span style="color:rgb(39,42,216)">0</span><span> + tuple.</span><span style="color:rgb(39,42,216)">1</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div><span><br></span></div></div></div><div><font face="Menlo"><span style="font-size:11px">tuple</span></font> is of type <span style="font-family:Menlo;font-size:11px">(</span><span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px">Int</span><span style="font-family:Menlo;font-size:11px">, </span><span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px">Int</span><span style="font-family:Menlo;font-size:11px">).</span></div><div><br></div><div>The problem is that the calculation is not very comprehensible due to <span style="font-family:Menlo;font-size:11px">.</span><span style="font-family:Menlo;font-size:11px;color:rgb(39,42,216)">0 </span>and <span style="font-family:Menlo;font-size:11px">.</span><span style="font-family:Menlo;font-size:11px;color:rgb(39,42,216)">1</span>. That’s when destructuring tuples directly in the parameter list comes into play:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span><span> c = </span><span style="color:rgb(61,29,129)">zip</span><span>(</span><span style="color:rgb(79,129,135)">a</span><span>,</span><span style="color:rgb(79,129,135)">b</span><span>).</span><span style="color:rgb(61,29,129)">reduce</span><span>(</span><span style="color:rgb(39,42,216)">0</span><span>) { acc, </span><span>(valueA, valueB)<span> </span><span style="color:rgb(187,44,162)">in</span></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>  acc + </span><span>valueA + </span>valueB</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div></div><div><br></div><div>The above is what I propose should be accepted by the compiler (but currently isn’t).</div><div><br></div><div>Currently tuple destructuring is possible like this:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">let</span><span> c = zip(a,b).reduce(</span><span style="color:#272ad8">0</span><span>) { (acc, tuple) </span><span style="color:#bb2ca2">in</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>  </span><span style="color:#bb2ca2">let</span><span> (valueA, valueB) = tuple</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>  </span><span style="color:rgb(187,44,162)">return</span> acc + valueA + valueB</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div></div><div><br></div><div>This is not about saving one line ;-). I just find it much more intuitive to destructure the tuple in the parameter list itself.</div><div><br></div><div>The same thing could be done for functions:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">func</span><span> takesATuple(someInt: </span><span style="color:#703daa">Int</span><span>, tuple: (</span><span style="color:#703daa">String</span><span>, </span><span style="color:#703daa">String</span><span>))</span></div></div><div><br></div><div>Here we also need to destructure the tuple inside the function, but the intuitive place (at least for me) to do this would be the parameter list.</div><div><br></div><div>In the following example I&#39;m making use of Swift’s feature to name parameters different from their labels (for internal use inside the function, this is not visible to consumers of the API):</div><div><br></div><div><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">func</span><span style="font-family:Menlo;font-size:11px"> takesATuple(someInt: </span><span style="font-family:Menlo;font-size:11px;color:rgb(112,61,170)">Int</span><span style="font-family:Menlo;font-size:11px">, tuple (valueA, valueB): (<span style="color:rgb(112,61,170)">String</span>, <span style="color:rgb(112,61,170)">String</span>))</span></div><div><br></div><div>Here <span style="font-family:Menlo;font-size:11px">valueA</span> and <span style="font-family:Menlo;font-size:11px">valueB</span> would be directly usable within the function. The tuple as a whole would not be available anymore.</div><div><br></div><div><br></div><div>Now it’s your turn!</div><div><br></div><div>1. What do you think?</div><div>2. Is this worth being discussed now (i.e. is it implementable in the Swift 3 timeframe) or should I delay it?</div><div><br></div><div>Cheers,</div><br><div>
<div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div>- Dennis</div></div></div></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>