<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 9, 2015, at 8:47 AM, David Owens II &lt;<a href="mailto:david@owensd.io" class="">david@owensd.io</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Another language construct seems a bit much for this, right? Maybe I’m missing something, but can’t we get the same behavior with an overload?</div></div></blockquote><div><br class=""></div><div>A language construct that helps eliminate multiple other language constructs would potentially be a net win, though. If there were closures that supported nonlocal exits, then `do`, `for...in`, and possibly other constructs could become library functions, and other "block-like" library features like `autoreleasepool`, `withUnsafePointer` would work more naturally too.</div><div><br class=""></div><div>-Joe</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><font face="Menlo" class="">extension&nbsp;Array&nbsp;{</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;func&nbsp;forEach&lt;U&gt;(body: (element: Element)&nbsp;throws&nbsp;-&gt; U?)&nbsp;rethrows&nbsp;-&gt;&nbsp;U? {</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;for&nbsp;e&nbsp;in&nbsp;self&nbsp;{</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;let&nbsp;result =&nbsp;try&nbsp;body(element: e) {&nbsp;return&nbsp;result }</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;nil</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;}</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">func&nbsp;g(e:&nbsp;Int) -&gt;&nbsp;Int? {</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;if&nbsp;e ==&nbsp;2&nbsp;{&nbsp;return&nbsp;e }</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;return&nbsp;nil</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">let&nbsp;arr = [1,&nbsp;2,&nbsp;3]</font></div><div class=""><font face="Menlo" class="">arr.forEach&nbsp;{&nbsp;print($0) }</font></div><div class=""><font face="Menlo" class="">let&nbsp;result =&nbsp;arr.forEach(g)</font></div><div class=""><font face="Menlo" class="">result &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // has the value of 2</font></div></blockquote><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Now, Swift has some issues determining the types properly if you attempt to inline the g function at the forEach() callsite, but that can be fixed.</div><div class=""><br class=""></div><div class="">-David</div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 9, 2015, at 4:40 AM, Stephen Celis via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><blockquote type="cite" class="">On Dec 8, 2015, at 5:13 PM, Joe Groff via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">Another direction you might take this is to make it a type annotation on the function type, like throws, so forEach has a type like this:<br class=""><br class="">func forEach(body: (Element) breaks -&gt; ())<br class=""><br class="">and a closure that `breaks` has nonlocal behavior for break/continue/return (and is implied to be noescape and void-returning, I guess).<br class=""></blockquote><br class="">This is really interesting. Ruby provides similar functionality with its lambda vs. proc, but a type annotation is much more understandable. It could also imply @noescape automatically:<br class=""><br class=""> &nbsp;&nbsp;&nbsp;func forEach(@canbreak body: Element -&gt; Void)<br class=""><br class="">Stephen<br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></body></html>