<div dir="ltr">To me it would seem more logical as &quot;for x in array? { }&quot; — to parallel &quot;for case let x? in array { }&quot;<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. <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>    for item in collection<br>    {<br>    }<br>}<br></div></div></blockquote>I&#39;ve been thinking about that lately, but haven&#39;t had the time to look wether someone on evolution already proposed a &quot;for in?&quot;-loop…</div><div><br></div><div>Imho the &quot;forEach&quot; solution is flawed, because you can&#39;t break the loop, and the &quot;?? []&quot; isn&#39;t perfect either:</div><div>I hope the compiler can optimise so that the assembly is as fast as the &quot;if let&quot; 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&#39;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"> i </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">in?</span><span style="font-variant-ligatures:no-common-ligatures"> test</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><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>