<div dir="ltr">I tend to write this kind of treatment the other way around...<div><br></div><div><div style="font-size:12.800000190734863px">if names.isEmpty {</div><div style="font-size:12.800000190734863px"><span class="gmail-m_-6364621347316393916Apple-tab-span" style="white-space:pre-wrap">        </span>// do whatever</div><div style="font-size:12.800000190734863px">} // 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">else {</div><div><div style="font-size:12.800000190734863px"><span style="white-space:pre-wrap">        </span>for name in names {</div><div style="font-size:12.800000190734863px"><span class="gmail-m_-6364621347316393916Apple-tab-span" style="white-space:pre-wrap">                </span>// do your thing</div><div style="font-size:12.800000190734863px"><span style="white-space:pre-wrap">        </span>}</div><div style="font-size:12.800000190734863px">}</div><div style="font-size:12.800000190734863px"><br></div><div style="font-size:12.800000190734863px"><br></div><div style="font-size:12.800000190734863px">Nicolas Fezans</div><div style="font-size:12.800000190734863px"><br></div><div style="font-size:12.800000190734863px"><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Feb 1, 2017 at 6:31 PM, Saagar Jha 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">If you’re fine with a couple extra characters, you can use .isEmpty:<div><br></div><div>for name in names {</div><div><span class="m_-6364621347316393916Apple-tab-span" style="white-space:pre-wrap">        </span>// do your thing</div><div>}</div><div>if names.isEmpty {</div><div><span class="m_-6364621347316393916Apple-tab-span" style="white-space:pre-wrap">        </span>// do whatever</div><div>}</div><div><br></div><div>It’s a bit more typing, but I feel it makes your intentions more clear.</div><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">Saagar Jha</div>

</div>
<br><div><blockquote type="cite"><div><div class="h5"><div>On Feb 1, 2017, at 8:48 AM, Chris Davis via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_-6364621347316393916Apple-interchange-newline"></div></div><div><div><div class="h5"><div style="word-wrap:break-word">Hi,<div><br></div><div>Often when I’m programming I stumble upon this scenario:</div><div><br></div><div>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><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">class</span><span style="font-variant-ligatures:no-common-ligatures"> Example_1</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">{</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">let</span><span style="font-variant-ligatures:no-common-ligatures"> names = [</span><span style="font-variant-ligatures:no-common-ligatures">&quot;Chris&quot;</span><span style="font-variant-ligatures:no-common-ligatures">, </span><span style="font-variant-ligatures:no-common-ligatures">&quot;John&quot;</span><span style="font-variant-ligatures:no-common-ligatures">, </span><span style="font-variant-ligatures:no-common-ligatures">&quot;Jordan&quot;</span><span style="font-variant-ligatures:no-common-ligatures">]</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </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)"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures">/// Loop over names, if no names, print no names</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">func</span><span style="font-variant-ligatures:no-common-ligatures"> run()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">for</span><span style="font-variant-ligatures:no-common-ligatures"> name </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">in</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">names</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">            </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81">print</span><span style="font-variant-ligatures:no-common-ligatures">(name)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">        </span><br class="m_-6364621347316393916webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">if</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">names</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">count</span><span style="font-variant-ligatures:no-common-ligatures"> == </span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8">0</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">            </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81">print</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#d12f1b">&quot;no names&quot;</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br><span style="font-variant-ligatures:no-common-ligatures"></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">let</span><span style="font-variant-ligatures:no-common-ligatures"> exampleOne = </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">Example_1</span><span style="font-variant-ligatures:no-common-ligatures">()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures">exampleOne</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d">run</span><span style="font-variant-ligatures:no-common-ligatures">()</span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures">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)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">class</span><span style="font-variant-ligatures:no-common-ligatures"> Example_2_Proposed</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">{</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">let</span><span style="font-variant-ligatures:no-common-ligatures"> names:[</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">String</span><span style="font-variant-ligatures:no-common-ligatures">] = []</span></div><div style="margin:0px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span><br class="m_-6364621347316393916webkit-block-placeholder"></div><div style="margin:0px;line-height:normal;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures">/// Loop over names, if no names, print no names</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">func</span><span style="font-variant-ligatures:no-common-ligatures"> run()</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    {</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">for</span><span style="font-variant-ligatures:no-common-ligatures"> name </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">in</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">names</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        {</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">            </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81">print</span><span style="font-variant-ligatures:no-common-ligatures">(name)</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        } </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">else</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">            </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81">print</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#d12f1b">&quot;no names&quot;</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        }</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    }</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">let</span><span style="font-variant-ligatures:no-common-ligatures"> exampleTwo = </span><span style="font-variant-ligatures:no-common-ligatures">Example_2_Proposed</span><span style="font-variant-ligatures:no-common-ligatures">()</span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures">exampleTwo</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d">run</span><span style="font-variant-ligatures:no-common-ligatures">()</span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures">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)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures">What would be the pros/cons of introducing such syntax?</span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures">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)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures">Thanks</span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures">Chris</span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div></span></div></div></div></div><span class="">______________________________<wbr>_________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br></span></div></blockquote></div><br></div></div><br>______________________________<wbr>_________________<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/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div>