<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="">Can’t most of this functionality be achieved with current Swift features, though? I mean, you could have the <span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">break</span> within the type that the closure returns.<br class=""><div class=""><br class=""></div><div class="">In the case of <span style="color: rgb(61, 29, 129); font-family: Menlo; font-size: 11px;" class="">forEach</span>, you could have it return a <span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Bool</span>, rather than <span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Void</span>, with <span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">true</span> signifying <span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">break</span>:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>SequenceType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> forEachBreak(body: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Generator</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Element</span> -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Bool</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> element <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> body(element) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">[<span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">1</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">2</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">3</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">4</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">5</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">6</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">7</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">8</span>].<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">forEachBreak</span> { n <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span>(n)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> n > <span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">4</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">true</span> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>return<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>false</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="font-family: Helvetica; font-size: 12px;" class="">For methods like <span style="font-family: Menlo; font-size: 11px;" class="">map</span>, you could have a version which takes a closure that returns an <span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Optional</span>: <span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">nil</span> signifying a break. Or, You could add something like a <span style="color: rgb(49, 89, 93); font-family: Menlo; font-size: 11px;" class="">takeWhile</span> method to <span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">SequenceType</span>. (which would probably be the easiest to understand)</div><div style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px;" class="">I think that annotations like <span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">@noescape</span> and <span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">throws</span> have huge overhead in terms of added confusion, especially for beginners. If the objective is to improve the understandability of certain functions, I think that something like this:</div><div style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px;" class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">sequence</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">takeWhile</span> { $0 < <span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">10</span> }.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">forEach</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span>($0) }</div></div><div style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px;" class="">is a better option than swelling the signature of <span style="color: rgb(61, 29, 129); font-family: Menlo; font-size: 11px;" class="">forEach</span> to:</div><div style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px;" class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> forEach(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">@noescape</span> body: (<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span>.Generator.Element) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">throws</span>, <span style="color: rgb(187, 44, 162);" class="">breaks</span> -> ()) <span style="color: rgb(187, 44, 162);" class="">rethrows</span></div></div><div style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px;" class="">Oisin.</div><div style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></div></div><div class=""><div><blockquote type="cite" class=""><div class="">On 9 Dec 2015, at 17:43, Matthew Johnson 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=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" 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></blockquote></blockquote></blockquote></blockquote><br class="">I missed this post the first time around. Is a really great idea and is something I hoped we might get eventually. It allows "control flow" functions to behave as expected.<br class=""><br class="">I assume a caller could pass a non-breaking closure if desired just as we can pass a non-throwing closure if desired, right?<br class="">,<br class="">How would break and continue interact with the caller of the closure? It would need to implement the correct behavior of skipping to the next loop cycle or moving on to any post-loop logic.<br class=""><br class="">How would this behave for a closure that is stored and called later, possibly asynchronously? Or would it only be allowed on closures declared<br class=""><br class="">Matthew<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></div></body></html>