<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Most alternatives already where discussed in swift-user:</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class="">Imho the "forEach" solution is flawed, because you can't break the loop, and the "?? []" isn't perfect either:</div><div class="">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 class=""><br class=""></div><div class="">This</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">extension</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">Optional</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">where</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> Wrapped: Sequence {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-tab-span" style="white-space: pre;">        </span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">var</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> elements: [</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">Wrapped</span><span class="" style="font-variant-ligatures: no-common-ligatures;">.Iterator.Element] {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162);"><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-tab-span" style="white-space: pre;">                </span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">switch</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> (</span><span class="" style="font-variant-ligatures: no-common-ligatures;">self</span><span class="" style="font-variant-ligatures: no-common-ligatures;">) {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-tab-span" style="white-space: pre;">                </span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">case</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> .</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(62, 30, 129);">none</span><span class="" style="font-variant-ligatures: no-common-ligatures;">:</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162);"><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-tab-span" style="white-space: pre;">                </span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">return</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> []</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-tab-span" style="white-space: pre;">                </span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">case</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> .</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(62, 30, 129);">some</span><span class="" style="font-variant-ligatures: no-common-ligatures;">(let o):</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-tab-span" style="white-space: pre;">                </span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">return</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">Array</span><span class="" style="font-variant-ligatures: no-common-ligatures;">(o)</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-tab-span" style="white-space: pre;">                </span>}</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-tab-span" style="white-space: pre;">        </span>}</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;">}</span></div><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;"><br class=""></span></div><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;"><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">let</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> test: [</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">Int</span><span class="" style="font-variant-ligatures: no-common-ligatures;">]? = </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">nil</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">for</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> i </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">in</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> test.</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">elements</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-tab-span" style="white-space: pre;">        </span>print</span><span class="" style="font-variant-ligatures: no-common-ligatures;">(i)</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;">}</span></div></span></div></div><div class=""><br class=""></div><div class="">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></blockquote></div><div class=""><div class=""><br class=""></div></div><br class=""><div class="">But maybe there is something better:</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="">for</span><span style="font-variant-ligatures: no-common-ligatures" class=""> i </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class=""> test? {</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 class="Apple-tab-span" style="white-space:pre">        </span></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" class="">i)</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 class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">if</span><span style="font-variant-ligatures: no-common-ligatures" class=""> i == </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">42</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(186, 45, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">                </span></span><span style="font-variant-ligatures: no-common-ligatures" class="">break</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 class="Apple-tab-span" style="white-space:pre">        </span>}</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><div class=""><br class=""></div><div class="">This doesn't work — but it compiles fine when you replace the "?" with "!".</div><div class=""><br class=""></div><div class="">I haven't proposed "in?" earlier because it would be a new keyword (even if its not that new…), and I'm very skeptical towards such changes.</div><div class="">But it is a very common pattern in Swift to have a "!" form that may crash and a save "?" variant, so I think it is actually missing in the syntax of for-loops.</div><div class=""><br class=""></div></body></html>