Using do/try/catch syntax is Swift seems to require too many lines of code. It adds so much ceremony in my code when there is a call which throws and I need to handle the error. There is no option to use a guard statement to compact the code a bit more. I'd like to use a Throw Coalescing Operator which does not exist the language. The solution I have come to is this attempt
function which will take an expression and wrap the do/try/catch syntax and pass the Swift.Error
to the fail
closure. It enables various uses which are listed in the samples. The goal was to make code which is more compact.
What I'd like to see implemented in the Swift language is a guard equivalent which passes the thrown error with one or more expressions to the else block.
attempt try writeFiles(), try transformFiles() else { error in
handleError(error)
}
In this code the try statements can be put together in the same expression and if either of them throw it will be passed to the else statement which could return or not. It could return a Bool as well to indicate success or failure to work well with branching logic.