<div dir="ltr">You can write a helper method that adds a placeholder if the array is empty:<div>    <br></div><div>    for name in names.placeholder(&quot;no names&quot;) {</div><div>        print(name)</div><div>    }</div><div><br></div><div>Implementation:</div><div><div><br></div><div>    extension Collection {</div><div>        func placeholder(_ elem: Iterator.Element) -&gt; PlaceholderView&lt;Self&gt; {</div><div>            return PlaceholderView(collection: self, ifEmpty: elem)</div><div>        }</div><div>    }</div><div><br></div><div>    enum PlaceholderView&lt;C: Collection&gt; : Collection {</div><div>        case original(C)</div><div>        case placeholder(C.Iterator.Element)</div><div>    <br></div><div>        init(collection: C, ifEmpty: C.Iterator.Element) {</div><div>            if (collection.isEmpty) {</div><div>                self = .placeholder(ifEmpty)</div><div>            } else {</div><div>                self = .original(collection)</div><div>            }</div><div>        }</div><div>    <br></div><div>        // implement Collection conformance</div><div>    }</div><div><div class="gmail_extra"><br><div class="gmail_quote">2017-02-01 19:48 GMT+03:00 Chris Davis via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><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:rgb(186,45,162)">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;color:rgb(0,0,0)">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">let</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> names = [</span><span style="font-variant-ligatures:no-common-ligatures">&quot;Chris&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">, </span><span style="font-variant-ligatures:no-common-ligatures">&quot;John&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">, </span><span style="font-variant-ligatures:no-common-ligatures">&quot;Jordan&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">]</span></div><p 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="gmail-m_-3661265389976255033webkit-block-placeholder"></p><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;color:rgb(0,0,0)">    </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:rgb(186,45,162)">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:rgb(186,45,162)">for</span><span style="font-variant-ligatures:no-common-ligatures"> name </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">in</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">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:rgb(62,30,129)">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><p 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="gmail-m_-3661265389976255033webkit-block-placeholder"></p><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:rgb(186,45,162)">if</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">names</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">count</span><span style="font-variant-ligatures:no-common-ligatures"> == </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">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:rgb(62,30,129)">print</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)">&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:rgb(186,45,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> exampleOne = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">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;color:rgb(0,0,0)">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">run</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">()</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;color:rgb(0,0,0)"><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;color:rgb(0,0,0)">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;color:rgb(0,0,0)"><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;color:rgb(0,0,0)"><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">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:rgb(186,45,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> names:[</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">String</span><span style="font-variant-ligatures:no-common-ligatures">] = []</span></div><p style="margin:0px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span><br class="gmail-m_-3661265389976255033webkit-block-placeholder"></p><div style="margin:0px;line-height:normal;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">    </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:rgb(186,45,162)">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:rgb(186,45,162)">for</span><span style="font-variant-ligatures:no-common-ligatures"> name </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">in</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">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:rgb(62,30,129)">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:rgb(186,45,162)">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:rgb(62,30,129)">print</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)">&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:rgb(186,45,162)">let</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> exampleTwo = </span><span style="font-variant-ligatures:no-common-ligatures">Example_2_Proposed</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">()</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;color:rgb(0,0,0)">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">run</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">()</span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"><br></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">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;color:rgb(0,0,0)"><br></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">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;color:rgb(0,0,0)"><br></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">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;color:rgb(0,0,0)"><br></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">Thanks</span></div><span class="gmail-HOEnZb"><font color="#888888"><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"><br></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">Chris</span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"><br></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"><br></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"><br></span></div><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"><br></span></div></font></span></span></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></div></div></div>