<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<Element, Return>(</div><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="">_</span> predicate: <span style="color: #ba2da2" class="">@escaping</span> (<span style="color: #4f8187" class="">Element</span>) <span style="color: #ba2da2" class="">throws</span> -> <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=""> do body: ((<span style="color: #4f8187" class="">Element</span>) -> <span style="color: #703daa" class="">Bool</span>) -> <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=""> ) </span>rethrows</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> -> <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=""> <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=""> <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=""> <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=""> <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=""> element = elem</div><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="">do</span> {</div><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="">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=""> }</div><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="">catch</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> 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=""> </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=""> </span>// Terminate search</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=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <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=""> <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=""> <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=""> <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=""> }</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=""> <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 <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> 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<Element, Return>(_ predicate: (Element) throws -> Bool, do body: ((Element) -> Bool) -> Return) rethrows -> Return {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> var caught: Error?<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> let value = body { elem in <br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> do {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> return try predicate(elem)<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> }<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> catch {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> caught = error<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> return true // Terminate search<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> }<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> }<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> <br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> if let caught = caught {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> throw caught<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> }<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> else {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> return value<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> }<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>