<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>&nbsp;within the type that the closure returns.<br class=""><div class=""><br class=""></div><div class="">In the case of&nbsp;<span style="color: rgb(61, 29, 129); font-family: Menlo; font-size: 11px;" class="">forEach</span>, you could have it return a&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Bool</span>, rather than&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Void</span>, with&nbsp;<span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">true</span>&nbsp;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="">&nbsp; <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> -&gt; <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="">&nbsp; &nbsp; <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="">&nbsp; &nbsp; &nbsp; <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="">&nbsp; &nbsp; &nbsp; &nbsp; <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="">&nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; }</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="">&nbsp; <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="">&nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> n &gt; <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="">&nbsp; </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&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">map</span>, you could have a version which takes a closure that returns an&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Optional</span>:&nbsp;<span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">nil</span>&nbsp;signifying a break. Or, You could add something like a&nbsp;<span style="color: rgb(49, 89, 93); font-family: Menlo; font-size: 11px;" class="">takeWhile</span>&nbsp;method to&nbsp;<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&nbsp;<span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">@noescape</span>&nbsp;and&nbsp;<span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">throws</span>&nbsp;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 &lt; <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&nbsp;<span style="color: rgb(61, 29, 129); font-family: Menlo; font-size: 11px;" class="">forEach</span>&nbsp;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>,&nbsp;<span style="color: rgb(187, 44, 162);" class="">breaks</span>&nbsp;-&gt; ()) <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 &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=""><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 -&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></blockquote></blockquote></blockquote></blockquote><br class="">I missed this post the first time around. &nbsp;Is a really great idea and is something I hoped we might get eventually. &nbsp;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? &nbsp;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? &nbsp;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>