[swift-users] Rethrows issue

Brent Royal-Gordon brent at brentdax.com
Sat Dec 30 22:15:33 CST 2017


I need to do something like this:

	func withPredicateErrors<Element, Return>(_ predicate: (Element) throws -> Bool, do body: ((Element) -> Bool) -> Return) rethrows -> Return {
	  var caught: Error?
	  let value = body { elem in 
	    do {
	      return try predicate(elem)
	    }
	    catch {
	      caught = error
	      return true     // Terminate search
	    }
	  }
	  
	  if let caught = caught {
	    throw caught
	  }
	  else {
	    return value
	  }
	}

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.

Is there any way to do this? Either to override the compiler's safety check, or to rewrite this function to avoid it?

-- 
Brent Royal-Gordon
Architechies



More information about the swift-users mailing list