<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">How about this?</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">func</span> withPredicateErrors&lt;Element, Return&gt;(</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">_</span> predicate: <span style="color: #ba2da2" class="">@escaping</span> (<span style="color: #4f8187" class="">Element</span>) <span style="color: #ba2da2" class="">throws</span> -&gt; <span style="color: #703daa" class="">Bool</span>,</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; do body: ((<span style="color: #4f8187" class="">Element</span>) -&gt; <span style="color: #703daa" class="">Bool</span>) -&gt; <span style="color: #4f8187" class="">Return</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class="">&nbsp; &nbsp; ) </span>rethrows</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; -&gt; <span style="color: #4f8187" class="">Return</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">var</span> caught: <span style="color: #703daa" class="">Error</span>?</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">var</span> element: <span style="color: #4f8187" class="">Element</span>?</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">let</span> value = body { elem <span style="color: #ba2da2" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; element = elem</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">do</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">return</span> <span style="color: #ba2da2" class="">try</span> predicate(elem)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">catch</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; caught = error</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ba2da2" class="">return</span><span style="color: #000000" class=""> </span><span style="color: #ba2da2" class="">true</span><span style="color: #000000" class=""> &nbsp; &nbsp; </span>// Terminate search</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">if</span> <span style="color: #ba2da2" class="">let</span> <span style="color: #ba2da2" class="">_</span> = caught,</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">let</span> element = element {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">try</span> <span style="color: #ba2da2" class="">_</span> = predicate(element)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 12px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">return</span> value</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div></div><div class=""><br class=""></div><div class="">-Kenny</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 30, 2017, at 8:15 PM, Brent Royal-Gordon via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">I need to do something like this:<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func withPredicateErrors&lt;Element, Return&gt;(_ predicate: (Element) throws -&gt; Bool, do body: ((Element) -&gt; Bool) -&gt; Return) rethrows -&gt; Return {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;var caught: Error?<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;let value = body { elem in <br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;&nbsp;&nbsp;do {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return try predicate(elem)<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;&nbsp;&nbsp;}<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;&nbsp;&nbsp;catch {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;caught = error<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true &nbsp;&nbsp;&nbsp;&nbsp;// Terminate search<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;&nbsp;&nbsp;}<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;}<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;if let caught = caught {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;&nbsp;&nbsp;throw caught<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;}<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;else {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;&nbsp;&nbsp;return value<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;}<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class=""><br class="">The problem is, the Swift compiler doesn't allow the explicit `throw` statement; even though it can only throw errors originally thrown by `predicate`, the compiler is not smart enough to prove that to itself. I cannot make `body` a `throws` function.<br class=""><br class="">Is there any way to do this? Either to override the compiler's safety check, or to rewrite this function to avoid it?<br class=""><br class="">-- <br class="">Brent Royal-Gordon<br class="">Architechies<br class=""><br class="">_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></div></blockquote></div><br class=""></body></html>