<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="">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 class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><font face="Menlo" class="">extension Array {</font></div><div class=""><font face="Menlo" class=""> func forEach<U>(body: (element: Element) throws -> U?) rethrows -> U? {</font></div><div class=""><font face="Menlo" class=""> for e in self {</font></div><div class=""><font face="Menlo" class=""> if let result = try body(element: e) { return result }</font></div><div class=""><font face="Menlo" class=""> }</font></div><div class=""><font face="Menlo" class=""> </font></div><div class=""><font face="Menlo" class=""> return nil</font></div><div class=""><font face="Menlo" class=""> }</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 g(e: Int) -> Int? {</font></div><div class=""><font face="Menlo" class=""> if e == 2 { return e }</font></div><div class=""><font face="Menlo" class=""> return 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 arr = [1, 2, 3]</font></div><div class=""><font face="Menlo" class="">arr.forEach { print($0) }</font></div><div class=""><font face="Menlo" class="">let result = arr.forEach(g)</font></div><div class=""><font face="Menlo" class="">result // 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><blockquote type="cite" class=""><div class="">On Dec 9, 2015, at 4:40 AM, Stephen Celis via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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 -> ())<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=""> func forEach(@canbreak body: Element -> 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="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></blockquote></div><br class=""></div></body></html>