- Proposal: SE-TBD
- Author(s): Soroush Khanlou
- Status: tbd
- Review manager: tbd
This proposal introduces a guard-catch statement to Swift, that is congruent to the existing guard-else statement with error catching support.
This proposal was first discussed on the Swift Evolution list in the TBD thread.
There is no natural way to incorporate a throwing request into a guard without applying try? and discarding the error. Introducing guard-catch allows you to retrieve a successful value and handle a failure without losing error information.
In the current Swift language design, you can only achieve this approach by:
- discarding the thrown error; or
- embedding the
guardstatement into ado-catchscope.
Neither produces a satisfactory or elegant solution for using throwing calls within a guard statement.
The statement derives from the guard-else grammar, substituting a catch clause for else, where the error is pre-defined and ready to use.
guard let value = try throwingFunction()
catch {
// handle error here, then leave scope
print (error); return
}
As with guard-else, the catch clause must leave scope after handling the error.
This change is purely additive.
Not incorporating this statement.