<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">ah! I forgot about the break semantics, that’s definitely one for the con list.<div class=""><br class=""></div><div class="">I like Nicolas’ solution, clear to read.<br class=""><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 1 Feb 2017, at 18:18, Nicolas Fezans <<a href="mailto:nicolas.fezans@gmail.com" class="">nicolas.fezans@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">I tend to write this kind of treatment the other way around...<div class=""><br class=""></div><div class=""><div style="font-size:12.800000190734863px" class="">if names.isEmpty {</div><div style="font-size:12.800000190734863px" class=""><span class="gmail-m_-6364621347316393916Apple-tab-span" style="white-space:pre-wrap">        </span>// do whatever</div><div style="font-size:12.800000190734863px" class="">} // on other cases I might have a few else-if to treat other cases that need special treament</div></div><div style="font-size:12.800000190734863px" class="">else {</div><div class=""><div style="font-size:12.800000190734863px" class=""><span style="white-space:pre-wrap" class="">        </span>for name in names {</div><div style="font-size:12.800000190734863px" class=""><span class="gmail-m_-6364621347316393916Apple-tab-span" style="white-space:pre-wrap">                </span>// do your thing</div><div style="font-size:12.800000190734863px" class=""><span style="white-space:pre-wrap" class="">        </span>}</div><div style="font-size:12.800000190734863px" class="">}</div><div style="font-size:12.800000190734863px" class=""><br class=""></div><div style="font-size:12.800000190734863px" class=""><br class=""></div><div style="font-size:12.800000190734863px" class="">Nicolas Fezans</div><div style="font-size:12.800000190734863px" class=""><br class=""></div><div style="font-size:12.800000190734863px" class=""><br class=""></div></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Wed, Feb 1, 2017 at 6:31 PM, Saagar Jha via swift-evolution <span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></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="">If you’re fine with a couple extra characters, you can use .isEmpty:<div class=""><br class=""></div><div class="">for name in names {</div><div class=""><span class="m_-6364621347316393916Apple-tab-span" style="white-space:pre-wrap">        </span>// do your thing</div><div class="">}</div><div class="">if names.isEmpty {</div><div class=""><span class="m_-6364621347316393916Apple-tab-span" style="white-space:pre-wrap">        </span>// do whatever</div><div class="">}</div><div class=""><br class=""></div><div class="">It’s a bit more typing, but I feel it makes your intentions more clear.</div><div class=""><br class=""><div class="">
<div style="letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; word-wrap: break-word;" class="">Saagar Jha</div>
</div>
<br class=""><div class=""><blockquote type="cite" class=""><div class=""><div class="h5"><div class="">On Feb 1, 2017, at 8:48 AM, Chris Davis via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:</div><br class="m_-6364621347316393916Apple-interchange-newline"></div></div><div class=""><div class=""><div class="h5"><div style="word-wrap:break-word" class="">Hi,<div class=""><br class=""></div><div class="">Often when I’m programming I stumble upon this scenario:</div><div class=""><br class=""></div><div class="">I have a list of items that may or may not be empty - if it’s full, I do one thing, if it’s empty I do something else, my code looks like this:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">class</span><span style="font-variant-ligatures:no-common-ligatures" class=""> Example_1</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">{</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> names = [</span><span style="font-variant-ligatures:no-common-ligatures" class="">"Chris"</span><span style="font-variant-ligatures:no-common-ligatures" class="">, </span><span style="font-variant-ligatures:no-common-ligatures" class="">"John"</span><span style="font-variant-ligatures:no-common-ligatures" class="">, </span><span style="font-variant-ligatures:no-common-ligatures" class="">"Jordan"</span><span style="font-variant-ligatures:no-common-ligatures" class="">]</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><br class="m_-6364621347316393916webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures" class="">/// Loop over names, if no names, print no names</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">func</span><span style="font-variant-ligatures:no-common-ligatures" class=""> run()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">for</span><span style="font-variant-ligatures:no-common-ligatures" class=""> name </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">in</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="">names</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81" class="">print</span><span style="font-variant-ligatures:no-common-ligatures" class="">(name)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><br class="m_-6364621347316393916webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">if</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="">names</span><span style="font-variant-ligatures:no-common-ligatures" class="">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="">count</span><span style="font-variant-ligatures:no-common-ligatures" class=""> == </span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="">0</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81" class="">print</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#d12f1b" class="">"no names"</span><span style="font-variant-ligatures:no-common-ligatures" class="">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><br class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> exampleOne = </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="">Example_1</span><span style="font-variant-ligatures:no-common-ligatures" class="">()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">exampleOne</span><span style="font-variant-ligatures:no-common-ligatures" class="">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d" class="">run</span><span style="font-variant-ligatures:no-common-ligatures" class="">()</span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">However, Personally, I would find it more pleasing to write something like this:</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">class</span><span style="font-variant-ligatures:no-common-ligatures" class=""> Example_2_Proposed</span></div><div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">{</span></div><div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> names:[</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="">String</span><span style="font-variant-ligatures:no-common-ligatures" class="">] = []</span></div><div style="margin:0px;line-height:normal;min-height:13px" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><br class="m_-6364621347316393916webkit-block-placeholder"></div><div style="margin:0px;line-height:normal;color:rgb(0,132,0)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures" class="">/// Loop over names, if no names, print no names</span></div><div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">func</span><span style="font-variant-ligatures:no-common-ligatures" class=""> run()</span></div><div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> {</span></div><div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">for</span><span style="font-variant-ligatures:no-common-ligatures" class=""> name </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">in</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="">names</span></div><div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> {</span></div><div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81" class="">print</span><span style="font-variant-ligatures:no-common-ligatures" class="">(name)</span></div><div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> } </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">else</span><span style="font-variant-ligatures:no-common-ligatures" class=""> {</span></div><div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81" class="">print</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#d12f1b" class="">"no names"</span><span style="font-variant-ligatures:no-common-ligatures" class="">)</span></div><div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div><div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div><div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div><div style="margin:0px;line-height:normal;min-height:13px" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> exampleTwo = </span><span style="font-variant-ligatures:no-common-ligatures" class="">Example_2_Proposed</span><span style="font-variant-ligatures:no-common-ligatures" class="">()</span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">exampleTwo</span><span style="font-variant-ligatures:no-common-ligatures" class="">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d" class="">run</span><span style="font-variant-ligatures:no-common-ligatures" class="">()</span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">The difference here is a “for-else” type syntax where if there were no items in the array it would simply fall through to the else statement.</span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">What would be the pros/cons of introducing such syntax?</span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">Is there’s a way of doing something similar in swift already?</span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">Thanks</span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">Chris</span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div></span></div></div></div></div><span class="">______________________________<wbr class="">_________________<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/<wbr class="">mailman/listinfo/swift-<wbr class="">evolution</a><br class=""></span></div></blockquote></div><br class=""></div></div><br class="">______________________________<wbr 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/<wbr class="">mailman/listinfo/swift-<wbr class="">evolution</a><br class="">
<br class=""></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></div></body></html>