I haven't got a general solution, but in this specific case, a "side effect" of map
can be used:
try error.map { throw $0 }
Or a throw()
method can be added:
extension Optional where Wrapped: Error {
/// Unwraps and throws the wrapped error, if the instance isn't `nil`.
///
/// let error: Error?
/// /* ... */
/// try error.throw()
///
/// - Throws: The wrapped error.
public func `throw`() throws {
try self.map { throw $0 }
}
}
Or instead of using map
, a forValue
or ifPresent
API can be added: