<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>How would you write&nbsp;</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">for x in array as? &lt;something&gt;</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">With parentheses? I like "in?" because it mimics "as?".<br><br>Sent from my iPhone</div><div><br>On Feb 10, 2017, at 13:57, Jacob Bandes-Storch &lt;<a href="mailto:jtbandes@gmail.com">jtbandes@gmail.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">To me it would seem more logical as "for x in array? { }" — to parallel "for case let x? in array { }"<div class="gmail_extra"><div><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><br></div></div></div></div>
<br><div class="gmail_quote">On Fri, Feb 10, 2017 at 1:03 PM, Rick Mann via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@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 dir="auto"><div>I love the idea of for in? (Or even for? In). You should pitch that to evolution.&nbsp;<br><br>Sent from my iPhone</div><div><div class="h5"><div><br>On Feb 10, 2017, at 07:04, Tino Heth &lt;<a href="mailto:2th@gmx.de" target="_blank">2th@gmx.de</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div><br><blockquote type="cite"><div><div>Is there any concise way to write the following?<br><br>if let collection = someOptionalCollection<br>{<br> &nbsp;&nbsp;&nbsp;for item in collection<br> &nbsp;&nbsp;&nbsp;{<br> &nbsp;&nbsp;&nbsp;}<br>}<br></div></div></blockquote>I've been thinking about that lately, but haven't had the time to look wether someone on evolution already proposed a "for in?"-loop…</div><div><br></div><div>Imho the "forEach" solution is flawed, because you can't break the loop, and the "?? []" isn't perfect either:</div><div>I hope the compiler can optimise so that the assembly is as fast as the "if let" solution, but even if this is the case, it is not obvious for a human reader.</div><div><br></div><div>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">extension</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Optional</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">where</span><span style="font-variant-ligatures:no-common-ligatures"> Wrapped: Sequence {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><span class="m_-5286311659184707977Apple-tab-span" style="white-space:pre-wrap">        </span></span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">var</span><span style="font-variant-ligatures:no-common-ligatures"> elements: [</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">Wrapped</span><span style="font-variant-ligatures:no-common-ligatures">.Iterator.Element] {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(186,45,162)"><span style="font-variant-ligatures:no-common-ligatures;color:#000000"><span class="m_-5286311659184707977Apple-tab-span" style="white-space:pre-wrap">                </span></span><span style="font-variant-ligatures:no-common-ligatures">switch</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> (</span><span style="font-variant-ligatures:no-common-ligatures">self</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">) {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><span class="m_-5286311659184707977Apple-tab-span" style="white-space:pre-wrap">                </span></span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">case</span><span style="font-variant-ligatures:no-common-ligatures"> .</span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81">none</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(186,45,162)"><span style="font-variant-ligatures:no-common-ligatures;color:#000000"><span class="m_-5286311659184707977Apple-tab-span" style="white-space:pre-wrap">                </span></span><span style="font-variant-ligatures:no-common-ligatures">return</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> []</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><span class="m_-5286311659184707977Apple-tab-span" style="white-space:pre-wrap">                </span></span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">case</span><span style="font-variant-ligatures:no-common-ligatures"> .</span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81">some</span><span style="font-variant-ligatures:no-common-ligatures">(let o):</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><span class="m_-5286311659184707977Apple-tab-span" style="white-space:pre-wrap">                </span></span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Array</span><span style="font-variant-ligatures:no-common-ligatures">(o)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><span class="m_-5286311659184707977Apple-tab-span" style="white-space:pre-wrap">                </span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><span class="m_-5286311659184707977Apple-tab-span" style="white-space:pre-wrap">        </span>}</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><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><span style="font-variant-ligatures:no-common-ligatures"><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"> test: [</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Int</span><span style="font-variant-ligatures:no-common-ligatures">]? = </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">nil</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">for</span><span style="font-variant-ligatures:no-common-ligatures"> i </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">in</span><span style="font-variant-ligatures:no-common-ligatures"> test.</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">elements</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 class="m_-5286311659184707977Apple-tab-span" style="white-space:pre-wrap">        </span>print</span><span style="font-variant-ligatures:no-common-ligatures">(i)</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></span></div></div><div><br></div><div>looks nice to me (except the return type — I guess there are better options), but I don't expect that the compiler can do much to optimise it.</div><div><br></div><div><div><span style="font-variant-ligatures:no-common-ligatures"><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)">for</span><span style="font-variant-ligatures:no-common-ligatures">&nbsp;i&nbsp;</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">in?</span><span style="font-variant-ligatures:no-common-ligatures">&nbsp;test</span><span style="font-variant-ligatures:no-common-ligatures">&nbsp;{</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><span class="m_-5286311659184707977Apple-tab-span" style="white-space:pre-wrap">        </span>print</span><span style="font-variant-ligatures:no-common-ligatures">(i)</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><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div>Imho looks even better, but this would need an extension of the language itself…</div><div><br></div></span></div></div></div></blockquote></div></div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div></div>
</div></blockquote></body></html>