<html><body><div>+1<br></div><div><br data-mce-bogus="1"></div><div>I do like the syntax suggested by Dennis. Making use of Swift's ability to differentiate between external and internal parameter names is a great idea!<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>-Thorsten<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div><br>Am 06. Mai 2016 um 06:25 schrieb "T.J. Usiyan via swift-evolution" <swift-evolution@swift.org>:<br><br><div><blockquote type="cite"><div class="msg-quote"><div dir="ltr">+1 <div>I have wanted this since the first beta. I hadn't proposed because I haven't come up with a nice syntax to do this in functions/methods. I don't particularly like</div><div><div style="font-size: 13px;" data-mce-style="font-size: 13px;"><span style="font-family: 'menlo'; font-size: 11px; color: #bb2ca2;" data-mce-style="font-family: 'menlo'; font-size: 11px; color: #bb2ca2;"> func</span><span style="font-family: 'menlo'; font-size: 11px;" data-mce-style="font-family: 'menlo'; font-size: 11px;"> takesATuple(someInt: </span><span style="font-family: 'menlo'; font-size: 11px; color: #703daa;" data-mce-style="font-family: 'menlo'; font-size: 11px; color: #703daa;">Int</span><span style="font-family: 'menlo'; font-size: 11px;" data-mce-style="font-family: 'menlo'; font-size: 11px;">, tuple (valueA, valueB): (<span style="color: #703daa;" data-mce-style="color: #703daa;">String</span>, <span style="color: #703daa;" data-mce-style="color: #703daa;">String</span>))</span></div></div><div><span style="font-family: Menlo;" data-mce-style="font-family: Menlo;" face="Menlo"><span style="font-size: 11px;" data-mce-style="font-size: 11px;"><br></span></span></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;" data-mce-style="font-size: 13px;"><span style="font-family: 'menlo'; font-size: 11px; color: #bb2ca2;" data-mce-style="font-family: 'menlo'; font-size: 11px; color: #bb2ca2;"> func</span><span style="font-family: 'menlo'; font-size: 11px;" data-mce-style="font-family: 'menlo'; font-size: 11px;"> takesATuple(someInt: </span><span style="font-family: 'menlo'; font-size: 11px; color: #703daa;" data-mce-style="font-family: 'menlo'; font-size: 11px; color: #703daa;">Int</span><span style="font-family: 'menlo'; font-size: 11px;" data-mce-style="font-family: 'menlo'; font-size: 11px;">, tuple: (<span style="color: #703daa;" data-mce-style="color: #703daa;">String</span>, <span style="color: #703daa;" data-mce-style="color: #703daa;">String</span>)) { </span><span style="font-family: 'menlo'; font-size: 11px;" data-mce-style="font-family: 'menlo'; font-size: 11px;"> (someInt, </span><span style="font-family: 'menlo'; font-size: 11px;" data-mce-style="font-family: 'menlo'; font-size: 11px;">(valueA, valueB)) in</span></div></div><div style="font-size: 13px;" data-mce-style="font-size: 13px;"><span style="font-family: 'menlo'; font-size: 11px;" data-mce-style="font-family: 'menlo'; font-size: 11px;"><br></span></div><div><span style="font-family: Menlo;" data-mce-style="font-family: Menlo;" face="Menlo"><span style="font-size: 11px;" data-mce-style="font-size: 11px;">but that gets confusing in my opinion, specifically if you choose to have different names inside and outside.</span></span></div><div><span style="font-family: 'menlo'; font-size: 11px;" data-mce-style="font-family: 'menlo'; font-size: 11px;"><br></span></div><div><span style="font-family: 'menlo'; font-size: 11px;" data-mce-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"><<a href="mailto:swift-evolution@swift.org" data-mce-href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0 0 0 0.8ex; border-left: 1px #ccc solid; padding-left: 1ex;" data-mce-style="margin: 0 0 0 0.8ex; border-left: 1px #ccc solid; padding-left: 1ex;"><div style="word-wrap: break-word;" data-mce-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" data-mce-href="http://thread.gmane.org/gmane.comp.lang.swift.user/1812">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';" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'menlo';"><span style="color: #bb2ca2;" data-mce-style="color: #bb2ca2;">let</span><span> a = [</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">0</span><span>,</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">1</span><span>,</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">2</span><span>,</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">3</span><span>,</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">4</span><span>,</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">5</span><span>,</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">6</span><span>,</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">7</span><span>,</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">8</span><span>,</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">9</span><span>]</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'menlo';" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'menlo';"><span style="color: #bb2ca2;" data-mce-style="color: #bb2ca2;">let</span><span> b = [</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">0</span><span>,</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">1</span><span>,</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">2</span><span>,</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">3</span><span>,</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">4</span><span>,</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">5</span><span>,</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">6</span><span>,</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">7</span><span>,</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">8</span><span>,</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">9</span><span>]</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'menlo'; min-height: 13px;" data-mce-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';" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'menlo';"><span style="color: #bb2ca2;" data-mce-style="color: #bb2ca2;">let</span><span> c = </span><span style="color: #3d1d81;" data-mce-style="color: #3d1d81;">zip</span><span>(</span><span style="color: #4f8187;" data-mce-style="color: #4f8187;">a</span><span>,</span><span style="color: #4f8187;" data-mce-style="color: #4f8187;">b</span><span>).</span><span style="color: #3d1d81;" data-mce-style="color: #3d1d81;">reduce</span><span>(</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">0</span><span>) { acc, tuple</span><span> </span><span style="color: #bb2ca2;" data-mce-style="color: #bb2ca2;">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'menlo';" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'menlo';"><span> acc + tuple.</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">0</span><span> + tuple.</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">1</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'menlo';" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'menlo';"><span>}</span></div><div><span><br></span></div></div></div><div><span style="font-family: Menlo;" data-mce-style="font-family: Menlo;" face="Menlo"><span style="font-size: 11px;" data-mce-style="font-size: 11px;">tuple</span></span> is of type <span style="font-family: 'menlo'; font-size: 11px;" data-mce-style="font-family: 'menlo'; font-size: 11px;">(</span><span style="color: #703daa; font-family: 'menlo'; font-size: 11px;" data-mce-style="color: #703daa; font-family: 'menlo'; font-size: 11px;">Int</span><span style="font-family: 'menlo'; font-size: 11px;" data-mce-style="font-family: 'menlo'; font-size: 11px;">, </span><span style="color: #703daa; font-family: 'menlo'; font-size: 11px;" data-mce-style="color: #703daa; font-family: 'menlo'; font-size: 11px;">Int</span><span style="font-family: 'menlo'; font-size: 11px;" data-mce-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;" data-mce-style="font-family: 'menlo'; font-size: 11px;">.</span><span style="font-family: 'menlo'; font-size: 11px; color: #272ad8;" data-mce-style="font-family: 'menlo'; font-size: 11px; color: #272ad8;">0 </span>and <span style="font-family: 'menlo'; font-size: 11px;" data-mce-style="font-family: 'menlo'; font-size: 11px;">.</span><span style="font-family: 'menlo'; font-size: 11px; color: #272ad8;" data-mce-style="font-family: 'menlo'; font-size: 11px; color: #272ad8;">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';" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'menlo';"><span style="color: #bb2ca2;" data-mce-style="color: #bb2ca2;">let</span><span> c = </span><span style="color: #3d1d81;" data-mce-style="color: #3d1d81;">zip</span><span>(</span><span style="color: #4f8187;" data-mce-style="color: #4f8187;">a</span><span>,</span><span style="color: #4f8187;" data-mce-style="color: #4f8187;">b</span><span>).</span><span style="color: #3d1d81;" data-mce-style="color: #3d1d81;">reduce</span><span>(</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">0</span><span>) { acc, </span><span>(valueA, valueB)<span> </span><span style="color: #bb2ca2;" data-mce-style="color: #bb2ca2;">in</span></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'menlo';" data-mce-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';" data-mce-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';" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'menlo';"><span style="color: #bb2ca2;" data-mce-style="color: #bb2ca2;">let</span><span> c = zip(a,b).reduce(</span><span style="color: #272ad8;" data-mce-style="color: #272ad8;">0</span><span>) { (acc, tuple) </span><span style="color: #bb2ca2;" data-mce-style="color: #bb2ca2;">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'menlo';" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'menlo';"><span> </span><span style="color: #bb2ca2;" data-mce-style="color: #bb2ca2;">let</span><span> (valueA, valueB) = tuple</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'menlo';" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'menlo';"><span> </span><span style="color: #bb2ca2;" data-mce-style="color: #bb2ca2;">return</span> acc + valueA + valueB</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'menlo';" data-mce-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';" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'menlo';"><span style="color: #bb2ca2;" data-mce-style="color: #bb2ca2;">func</span><span> takesATuple(someInt: </span><span style="color: #703daa;" data-mce-style="color: #703daa;">Int</span><span>, tuple: (</span><span style="color: #703daa;" data-mce-style="color: #703daa;">String</span><span>, </span><span style="color: #703daa;" data-mce-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'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: #bb2ca2;" data-mce-style="font-family: 'menlo'; font-size: 11px; color: #bb2ca2;">func</span><span style="font-family: 'menlo'; font-size: 11px;" data-mce-style="font-family: 'menlo'; font-size: 11px;"> takesATuple(someInt: </span><span style="font-family: 'menlo'; font-size: 11px; color: #703daa;" data-mce-style="font-family: 'menlo'; font-size: 11px; color: #703daa;">Int</span><span style="font-family: 'menlo'; font-size: 11px;" data-mce-style="font-family: 'menlo'; font-size: 11px;">, tuple (valueA, valueB): (<span style="color: #703daa;" data-mce-style="color: #703daa;">String</span>, <span style="color: #703daa;" data-mce-style="color: #703daa;">String</span>))</span></div><div><br></div><div>Here <span style="font-family: 'menlo'; font-size: 11px;" data-mce-style="font-family: 'menlo'; font-size: 11px;">valueA</span> and <span style="font-family: 'menlo'; font-size: 11px;" data-mce-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: #000000; letter-spacing: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; word-wrap: break-word;" data-mce-style="color: #000000; letter-spacing: normal; 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" data-mce-href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" data-mce-href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br> <br></blockquote></div><br></div><div class="_stretch"><span class="body-text-content">_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" data-mce-href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" data-mce-href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></span></div></div></blockquote></div></div></body></html>