<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><blockquote type="cite" class="">On Dec 17, 2015, at 4:08 AM, Jeremy Pereira <<a href="mailto:jeremy.j.pereira@googlemail.com" class="">jeremy.j.pereira@googlemail.com</a>> wrote:<br class=""><br class=""><br class=""><blockquote type="cite" class="">On 16 Dec 2015, at 19:52, Paul Cantrell via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class="">I do this a lot:<br class=""><br class=""> 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 <i class="">lot</i> 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=""> <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=""> <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 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=""> </span><span style="font-size: 10.5px; color: rgb(50, 62, 125);" class="">for</span><span style="font-size: 10.5px;" class=""> item </span><span style="font-size: 10.5px; color: rgb(50, 62, 125);" class="">in</span><span style="font-size: 10.5px;" class=""> urlComponents.</span><span style="font-size: 10.5px; color: rgb(88, 126, 168);" class="">queryItems</span><span style="font-size: 10.5px;" class=""> ?? []</span></div><div style="margin: 0px; font-size: 10.5px; line-height: normal; font-family: Menlo;" class=""> { queryDict[item.<span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">name</span>] = item.<span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">value</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></body></html>