[swift-evolution] Idea: Extend "guard" to try-statements, with a catch block

Антон Жилин antonyzhilin at gmail.com
Sat Mar 12 06:17:16 CST 2016


By the way, you can write a helper for immediate handling of errors:

public func handle<T>(@autoclosure body: () throws -> T, errorHandler:
(ErrorType) -> ()) -> T? {
    do {
        return try body()
    } catch e {
        errorHandler(e)
        return nil
    }
}

func dangerousMethod() throws -> Int { /*...*/ }

let result: Int? = handle(try dangerousMethod()) { /*...*/ }
result ?? 0

guard let result = handle(try dangerousMethod(), log) {
    return
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160312/f664d28e/attachment.html>


More information about the swift-evolution mailing list