2016-08-06 2:50 GMT+03:00 Fernando Rodríguez <[email protected]>:

b) do/try/catch
>
> This allows you to have information about the error, but also causes the
> newly created object to be "trapped" inside a do block.
>
> Are there any plans to address this situation? I believe there should be a
> single, obvious and convenient way of handling errors in the language.
>
func attempt<T>(_ expression: @autoclosure () throws -> T, handler:
(ErrorProtocol) -> T) -> T {
    do {
        return try expression()
    } catch {
        return handler(error)
    }
}
let x = attempt(downloadDataOrThrow()) {
    print("Connection lost")
    return nil
}

It mimics guard statement. You can also have versions of attempt with
partially defined handlers, like “return nil“ or “print and crash”, or
“assert type of error”.
​
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to