<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div>The syntax could be</div><div>for x in array? {</div><div><br></div><div>}</div><div><br></div><div>It wouldn't necessarily be consistent with&nbsp;</div><div><br></div><div>if let foo = foo {</div><div><br></div><div>}</div><div><br></div><div>But as I've mentioned in another thread regarding the if let syntax and variable shadowing, it would probably make more sense to new users if the if let syntax was the following.</div><div><br></div><div>if let foo = foo? {</div><div><br></div><div>}</div><div><br></div><div>The ? is always used to conditionally unwrap optionals. It strikes me as odd that they are not used to unwrap them in control flow statements.&nbsp;</div><div><br></div><div>i.e. Why should&nbsp;</div><div><br></div><div>array?.forEach</div><div><br></div><div>be any different than,</div><div><br></div><div>for x in array?</div><div><br></div><div><br></div><div>Tyler</div><div><br>On Dec 20, 2015, at 12:15 PM, Marco Masser via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8"><div class="">I have to admit that Radek’s comment about&nbsp;“for … in? …” meaning an iteration over a SequenceType containing Optional Values is valid.</div><div class=""><br class=""></div><div class="">But I think “for? … in …” is worse because it separates the&nbsp;“?” from the thing that is the Optional thing. Consider:</div><div class=""><br class=""></div><div class="">for? x in array { … }</div><div class=""><br class=""></div><div class="">vs.</div><div class=""><br class=""></div><div class="">for x in? array { … }</div><div class=""><br class=""></div><div class="">If any of these two is about iterating over an Optional&lt;SequenceType&gt;, it’s the second one – at least to me. The first one reads much more like the&nbsp;“x” could be optional.</div><div class=""><br class=""></div><div class="">Marco</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On 2015-12-18, at 22:24, Radosław Pietruszewski via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">That’s… definitely an improvement as far as ambiguity is concerned, but I still don’t believe it passes the usefulness threshold, and I don’t really like the precedent of having a `for?`…</div><div class=""><br class=""></div><div class="">PS. FWIW, I like the spirit of the proposal, just not this solution. I’m all for “expressivity enhancements” — little things that helps me write cleaner code and express my intention better. But this doesn’t seem worth the trouble of extending the language.</div><br class=""><div class="">
<div class="">— Radek</div>
</div>
<br class=""><div class=""><blockquote type="cite" class=""><div class="">On 18 Dec 2015, at 22:20, Jacob Bandes-Storch &lt;<a href="mailto:jtbandes@gmail.com" class="">jtbandes@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">How about `for? object in array` instead?<div class="gmail_extra">
<br class=""><div class="gmail_quote">On Fri, Dec 18, 2015 at 1:13 PM, Paul Cantrell via swift-evolution <span dir="ltr" class="">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class=""><blockquote type="cite" class=""><div style="font-family:HelveticaNeue" class="">`<span style="background-color:rgb(255,255,255)" class=""><span style="white-space:pre-wrap" class="">for object in? array` … suggests that there’s something optional about checking for inclusion, not about the array itself. It could easily be interpreted as “iterate for all non-nil elements of array (where array: [T?])” —&nbsp;a use case arguably more common than this.</span></span></div></blockquote></div><div class=""><div style="font-family:HelveticaNeue" class=""><span style="background-color:rgb(255,255,255)" class=""><span style="white-space:pre-wrap" class=""><br class=""></span></span></div></div><div style="font-family:HelveticaNeue" class=""><span style="background-color:rgb(255,255,255)" class=""><span style="white-space:pre-wrap" class="">That’s a really good point.</span></span></div><div style="font-family:HelveticaNeue" class=""><span style="background-color:rgb(255,255,255)" class=""><span style="white-space:pre-wrap" class=""><br class=""></span></span></div><div style="font-family:HelveticaNeue" class=""><span style="background-color:rgb(255,255,255)" class=""><span style="white-space:pre-wrap" class="">P</span></span></div><div class=""><div class="h5"><div class=""><br class=""></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 18, 2015, at 3:06 PM, Radosław Pietruszewski &lt;<a href="mailto:radexpl@gmail.com" target="_blank" class="">radexpl@gmail.com</a>&gt; wrote:</div><br class=""><div class=""><div style="font-family:HelveticaNeue;font-size:13px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="">Personally, I’m -1 for the proposal. I see this as a solution to a very minor, fairly rare, and not generalizable problem.</div><div style="font-family:HelveticaNeue;font-size:13px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><br class=""></div><div style="font-family:HelveticaNeue;font-size:13px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="">Perhaps more importantly: the syntax is confusing to my eyes. `<span style="background-color:rgb(255,255,255)" class=""><span style="white-space:pre-wrap" class="">for object in? array` doesn’t immediately convey its semantics to me. It suggests that there’s something optional about checking for inclusion, not about the array itself. It could easily be interpreted as “iterate for all non-nil elements of array (where array: [T?])” —&nbsp;a use case arguably more common than this.</span></span></div><div style="font-family:HelveticaNeue;font-size:13px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><span style="background-color:rgb(255,255,255)" class=""><span style="white-space:pre-wrap" class=""><br class=""></span></span></div><div style="font-family:HelveticaNeue;font-size:13px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><span style="background-color:rgb(255,255,255)" class=""><span style="white-space:pre-wrap" class="">In the vast majority of cases, arrays shouldn’t be optional in the first place. It’s rare that there’s a semantic difference between “empty array” and “no array”.</span></span></div><div style="font-family:HelveticaNeue;font-size:13px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><span style="background-color:rgb(255,255,255)" class=""><span style="white-space:pre-wrap" class=""><br class=""></span></span></div><div style="font-family:HelveticaNeue;font-size:13px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><blockquote type="cite" class=""><div style="word-wrap:break-word" class=""><div class="">Sure, in that example it’s quite simple. It’s not the “?? []” syntax itself, which is perfectly clear; it’s having that dangling off the end of some longer expression. In real-world context, it does become additional noise.</div></div></blockquote><br class=""></div><div style="font-family:HelveticaNeue;font-size:13px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="">That is a good point, albeit one that’s more broad than that — I dislike how `as?` often forces me to add additional parentheses — and not strong enough to warrant an introduction of a new `in?` construct IMHO.</div><br style="font-family:HelveticaNeue;font-size:13px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><div style="font-family:HelveticaNeue;font-size:13px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><div class="">— Radek</div></div><br style="font-family:HelveticaNeue;font-size:13px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><div style="font-family:HelveticaNeue;font-size:13px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><blockquote type="cite" class=""><div class="">On 18 Dec 2015, at 21:56, Paul Cantrell via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class=""><div class=""><div style="word-wrap:break-word" class=""><br class=""><blockquote type="cite" class="">On Dec 17, 2015, at 4:08 AM, Jeremy Pereira &lt;<a href="mailto:jeremy.j.pereira@googlemail.com" target="_blank" class="">jeremy.j.pereira@googlemail.com</a>&gt; wrote:<br class=""><br class=""><br class=""><blockquote type="cite" class="">On 16 Dec 2015, at 19:52, Paul Cantrell via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">I do this a lot:<br class=""><br class="">&nbsp; &nbsp;for object in array ?? [] {<br class=""><br class="">…and it does impair readability a bit at times.<br class=""></blockquote><br class="">Does it? It seems fairly understandable to me even though I have never seen it before.<br class=""></blockquote><div class=""><br class=""></div><div class="">Sure, in that example it’s quite simple. It’s not the “?? []” syntax itself, which is perfectly clear; it’s having that dangling off the end of some longer expression. In real-world context, it does become additional noise.</div><div class=""><br class=""></div><blockquote type="cite" class="">I think there is a good reason for keeping this construct a bit “clunky”. Generally APIs give you a nil array for one of two reasons:<br class=""><br class="">- there was some sort of error in retrieving the elements<br class="">- there were no qualifying elements found.<br class=""></blockquote><div class=""><br class=""></div><div class="">You’re forgetting the third case, the most common one: things not populated / initialized yet. In that case, we often just want to leave a UI blank, for example, and doing nothing is the right behavior.</div><div class=""><br class=""></div><div class="">Doing nothing is the right behavior in a<span class="">&nbsp;</span><i class="">lot</i>&nbsp;of cases, and since Swift provides optional sugar for “do nothing if nil” in other cases:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo" class=""><span style="font-size:10.5px" class="">&nbsp; &nbsp;<span class="">&nbsp;</span><font color="#323e7d" class="">foo?.bar = baz</font></span></div></div><div class=""><div class=""><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo" class=""><span style="font-size:10.5px" class="">&nbsp; &nbsp;&nbsp;<font color="#323e7d" class="">if let foo = bar { }</font></span></div></div></div><div class=""><span style="font-size:10.5px" class=""><font color="#323e7d" class=""><br class=""></font></span></div><div class="">…it makes sense to provide it in for loops too.</div><div class=""><br class=""></div><div class="">True, “do nothing” is not the right behavior in all cases. Thus the `for…in` / `for … in?` distinction.</div><div class=""><br class=""></div><blockquote type="cite" class="">In the&nbsp;second case, the API is wrong.</blockquote><br class=""><div class="">Then a lot of APIs are wrong! NSURLComponents, for example:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo" class=""><span style="font-size:10.5px" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-size:10.5px;color:rgb(50,62,125)" class="">for</span><span style="font-size:10.5px" class=""><span class="">&nbsp;</span>item<span class="">&nbsp;</span></span><span style="font-size:10.5px;color:rgb(50,62,125)" class="">in</span><span style="font-size:10.5px" class="">&nbsp;urlComponents.</span><span style="font-size:10.5px;color:rgb(88,126,168)" class="">queryItems</span><span style="font-size:10.5px" class="">&nbsp;?? []</span></div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { queryDict[item.<span style="color:rgb(88,126,168)" class="">name</span>] = item.<span style="color:rgb(88,126,168)" class="">value</span><span class="">&nbsp;</span>}</div></div><div class=""><br class=""></div><div class="">But then I don’t think NSURLComponents is making a mistake: it distinguishes “no query string” from “empty query string”, but for the purposes of the loop here, there upshot in both cases is “no query items.”</div><div class=""><br class=""></div><div class="">True, ?? [] is minor noise — but given the clarity and low impact on existing code of a `for … in?` counterpart to `for … in`, it’s worth at least considering that option.</div><div class=""><br class=""></div><div class="">Cheers,</div><div class=""><br class=""></div><div class="">Paul</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=4hhvaxvZNsLrnZM9llg93kxoqfFYC8lhv8HAehOIDqKQXtEz44wzr57ueVaQ0SQlINAEWx-2FlQ6heG1BfClLuVCgQj6mVussRh0Fn12l9TvpgflEDodqORozjg-2BCZAnc76mhJLZ09jUOlHA6bL6DTPSKcp3oj78Exc-2Bj9UnzYb2mTXCGZyGarqO57AZOEfF49Q8wEqK1MqLFTqbYTqUPv5Fs1SbdgGmIgpMuyMXoEctE-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0px!important;margin:0px!important;padding:0px!important" class=""></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div></div></blockquote></div><br class="">
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=P-2BsYbBZHRBuLDBJaL4DIKDNfkkjpROowTyRAObV11qxr9GnJAiAwRYYfnZxtBrPCWd20WCXroFGcTo1EGQNkIQ-2FKeabTqBOYzLd4mWkdf8Bd2V755WfnM0UnVnqegqgoP-2FlpJDwTWg3zTQMHYvw82YIRsKQKF8SMu5xDfqiuYpqGoMdAmcoqx3UtTrnlPqW8w5bz4cujMtSKuGHJVvQloSMWP-2BDG5oB4uIOzF-2FnzaFo-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important" class="">
</div></div></div>
<br class="">_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
<br class=""></blockquote></div><br class=""></div></div>
</div></blockquote></div><br class="">
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=K9qmNlgbKPPmGnoyPaIDeacyynE-2BO94GI4Zq4mx9Z8RWynxV0ehaY9Yx7OC2qgVruES8gan-2FD-2BI7uc9No9l6EQXx80sB9Hx-2BxzX-2F-2FORNP2A4RYtQtrEnwi9lDclPhm3P1A-2FKQ5cRZyMiVJhsgcyQKWKLhUdYv1re8qI1QMSE37uswvqyooq9ZF8jG5uL-2F-2BEmxbVR7jsKSZTYz9CLF21PIRNCAjcuNUpqoSq05jbehB4-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
</div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class="">
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=DOmV4Qxg-2B1XOnX4CfTyH3sCllXs7myBcgXMCpdkqNW3lRG-2Fax3BJm8nLX4I5YpYhX1VBk4tH5xaTq98Xe6t-2FLYVUQcjVw5LnkhCrVEreDMUNuZ4wPtPTWBb7YDW9lHeWwYorQqwH-2BdEckmxtojOSIv8hed-2BsMVZu0z9T5-2B42Oc4OVqhhXbaTPGcBkhPe1bAjYa6mFGMIQDCQrqZqrzzYtngkn9Qjes22moDQPAOffYw-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;">

</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>