|
| 1 | +# Multi-Pattern and Conditionally Compiled Catch Clauses |
| 2 | + |
| 3 | +* Proposal: [SE-0269](0269-multi-pattern-and-conditionally-compiled-catches.md) |
| 4 | +* Authors: [Owen Voorhees](https://github.com/owenv) |
| 5 | +* Review Manager: TBD |
| 6 | +* Status: **Awaiting review** |
| 7 | +* Implementation: |
| 8 | + - [apple/swift#27776](https://github.com/apple/swift/pull/27776) (multi-pattern catch clauses) |
| 9 | + - [apple/swift#27905](https://github.com/apple/swift/pull/27905) (conditionally compiled catch clauses) |
| 10 | + |
| 11 | +## Introduction |
| 12 | + |
| 13 | +Currently, each catch clause in a do-catch statement may only contain a single pattern and where clause, and may not be conditionally compiled using a `#if` directive. This is inconsistent with the behavior of cases in switch statements which provide similar functionality. It also makes some error handling patterns awkward to express. This proposal extends the grammar of catch clauses to support `#if` and a comma-separated list of patterns (with optional where clauses), resolving this inconsistency. |
| 14 | + |
| 15 | +Swift-evolution thread: [Thread](https://forums.swift.org/t/multi-pattern-and-conditionally-compiled-catch-clauses/30246) |
| 16 | + |
| 17 | +## Motivation |
| 18 | + |
| 19 | +Currently, Swift only allows up to one pattern and where clause for each catch clause in a do-catch statement: |
| 20 | + |
| 21 | +```swift |
| 22 | +do { |
| 23 | + try performTask() |
| 24 | +} catch TaskError.someRecoverableError { // OK |
| 25 | + recover() |
| 26 | +} catch TaskError.someFailure(let msg), |
| 27 | + TaskError.anotherFailure(let msg) { // Not currently valid |
| 28 | + showMessage(msg) |
| 29 | +} |
| 30 | + |
| 31 | +``` |
| 32 | + |
| 33 | +Because the above snippet is not valid today, developers frequently end up duplicating code between catch clauses, or writing something like the following instead: |
| 34 | + |
| 35 | +``` |
| 36 | +do { |
| 37 | + try performTask() |
| 38 | +} catch { |
| 39 | + switch error { |
| 40 | + case TaskError.someRecoverableError: |
| 41 | + recover() |
| 42 | + case TaskError.someFailure(let msg), |
| 43 | + TaskError.anotherFailure(let msg): |
| 44 | + showMessage(msg) |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +Nesting the switch inside of the catch clause is awkward and introduces additional nesting. It also defeats the purpose of supporting pattern matching in catch clauses. Splitting the code up into multiple catch clauses requires duplication, which is also undesirable. Supporting a multi-pattern catch clause would allow for code which is both clearer and more concise. |
| 50 | + |
| 51 | +The following use of `#if` is also not allowed today and frequently leads to nesting of switch statements inside catch clauses: |
| 52 | + |
| 53 | +```swift |
| 54 | +do { |
| 55 | + try performTask() |
| 56 | +} |
| 57 | +#if os(macOS) |
| 58 | +catch TaskError.macOSOnlyError { |
| 59 | + macOSRecovery() |
| 60 | +} |
| 61 | +#endif |
| 62 | +catch { |
| 63 | + crossPlatformRecovery() |
| 64 | +} |
| 65 | + |
| 66 | +``` |
| 67 | + |
| 68 | + |
| 69 | +## Proposed solution |
| 70 | + |
| 71 | +Catch clauses should allow the user to specify a comma-separated list of patterns. If an error thrown at runtime in a do block matches any of the patterns in a corresponding catch clause, that catch clause should be executed. Similar to switch cases, a user should be able to bind a variable in all patterns of a catch clause and then use it in the body. |
| 72 | + |
| 73 | +Additionally, conditional compilation directives should be permitted to appear surrounding catch clauses. |
| 74 | + |
| 75 | +## Detailed design |
| 76 | + |
| 77 | +### Grammar Changes |
| 78 | + |
| 79 | +The revised catch clause grammar is as follows: |
| 80 | + |
| 81 | +``` |
| 82 | +catch-clauses -> catch-clause catch-clauses? |
| 83 | +
|
| 84 | +catch-clause -> 'catch' catch-item-list? code-block | |
| 85 | + conditional-catch-clause |
| 86 | +
|
| 87 | +catch-item-list -> catch-item | |
| 88 | + catch-item ',' case-item-list |
| 89 | +
|
| 90 | +catch-item -> pattern where-clause? | |
| 91 | + where-clause |
| 92 | +
|
| 93 | +conditional-catch-clause -> catch-if-directive-clause catch-elseif-directive-clauses? switch-else-directive-clause? endif-directive |
| 94 | +
|
| 95 | +catch-if-directive-clause -> if-directive compilation-condition catch-clauses? |
| 96 | +
|
| 97 | +catch-elseif-directive-clauses -> catch-elseif-directive-clause catch-elseif-directive-clauses? |
| 98 | +
|
| 99 | +catch-elseif-directive-clause -> elseif-directive compilation-condition catch-clauses? |
| 100 | +
|
| 101 | +catch-else-directive-clause -> else-directive catch-clauses? |
| 102 | +``` |
| 103 | + |
| 104 | +Note: Where clause expressions with trailing closures are not allowed in any of a catch clause's patterns. This differs from the behavior of switch cases. |
| 105 | + |
| 106 | +### Semantics |
| 107 | + |
| 108 | +If a catch clause has multiple patterns, then its body will be executed if a thrown error matches any one of those patterns, and has not already matched a pattern from a preceding catch clause. Similar to switch cases, catch clauses with multiple patterns may still contain value bindings. However, those bindings must have the same name and type in each pattern. |
| 109 | + |
| 110 | +Catch clauses within conditional compilation blocks work just like any other supported construct. The clause is parsed whether or not the provided condition evaluates to true, but is otherwise not compiled unless the condition evaluates to true. |
| 111 | + |
| 112 | +## Source compatibility |
| 113 | + |
| 114 | +This proposal maintains source compatibility. It will only result in code compiling which was considered invalid by older compiler versions. |
| 115 | + |
| 116 | +## Effect on ABI stability |
| 117 | + |
| 118 | +This feature has no ABI impact. |
| 119 | + |
| 120 | +## Effect on API resilience |
| 121 | + |
| 122 | +This proposal does not introduce any new features which could become part of a public API. |
| 123 | + |
| 124 | +## Alternatives considered |
| 125 | + |
| 126 | +### Do nothing |
| 127 | + |
| 128 | +These features are relatively minor additions to the language, and arguably would see rather limited usage. However, in the cases where they are needed, they help the user avoid hard-to-maintain error handling code which either duplicates functionality or nests control flow statements in a confusing manner. This proposal also simplifies Swift's pattern matching and conditional compilation models by unifying some of the semantics of switch and do-catch statements. |
| 129 | + |
| 130 | +## Future Directions |
| 131 | + |
| 132 | +There are a number of possible future directions which could increase the expressiveness of catch clauses. |
| 133 | + |
| 134 | +### Implicit `$error` or `error` binding for all catch clauses |
| 135 | + |
| 136 | +Currently, only catch clauses without a pattern have an implicit `error: Error` binding. However, there are some cases where it would be useful to have this binding in all catch clauses to make, for example, re-throwing errors easier. However, using `error` as the identifier for this binding would be a medium-to-large source-breaking change. Instead, we could continue the trend of compiler defined identifiers and use `$error`. `error` in empty catch clauses could then be deprecated and eventually removed in future language versions, a smaller source break. |
| 137 | + |
| 138 | +This change was not included in this proposal because it is source-breaking and orthogonal. If there is interest in this feature, we should probably consider it as an independent improvement which deserves its own proposal. |
| 139 | + |
| 140 | +### `fallthrough` support in catch clauses |
| 141 | + |
| 142 | +Allowing `fallthrough` statements in catch clauses would further unify the semantics of switch cases and catches. However, it is currently undesirable for a number of reasons. First, it would be a source-breaking change for existing do-catch statements which trigger `fallthrough`s in an enclosing switch. Also, there is no historical precedent from other languages for supporting `fallthrough`s in catch statements, unlike switches. Finally, there have not yet been any identified examples of code which would benefit from this functionality. |
0 commit comments