|
3 | 3 | > **Note**\
|
4 | 4 | > This is in reverse chronological order, so newer entries are added to the top.
|
5 | 5 |
|
| 6 | +## Swift 5.11 |
| 7 | + |
| 8 | +* [SE-0413][]: |
| 9 | + |
| 10 | + Functions can now specify the type of error that they throw as part of the |
| 11 | + function signature. For example: |
| 12 | + |
| 13 | + ```swift |
| 14 | + func parseRecord(from string: String) throws(ParseError) -> Record { ... } |
| 15 | + ``` |
| 16 | + |
| 17 | + A call to `parseRecord(from:)` will either return a `Record` instance or throw |
| 18 | + an error of type `ParseError`. For example, a `do..catch` block will infer |
| 19 | + the `error` variable as being of type `ParseError`: |
| 20 | + |
| 21 | + ```swift |
| 22 | + do { |
| 23 | + let record = try parseRecord(from: myString) |
| 24 | + } catch { |
| 25 | + // error has type ParseError |
| 26 | + } |
| 27 | + ``` |
| 28 | + |
| 29 | + Typed throws generalizes over throwing and non-throwing functions. A function |
| 30 | + that is specified as `throws` (without an explicitly-specified error type) is |
| 31 | + equivalent to one that specifies `throws(any Error)`, whereas a non-throwing |
| 32 | + is equivalent to one that specifies `throws(Never)`. Calls to functions that |
| 33 | + are `throws(Never)` are non-throwing. |
| 34 | + |
| 35 | + Typed throws can also be used in generic functions to propagate error types |
| 36 | + from parameters, in a manner that is more precise than `rethrows`. For |
| 37 | + example, the `Sequence.map` operation can propagate the thrown error type from |
| 38 | + its closure parameter, indicating that it only throws errors of the same type |
| 39 | + as that closure does: |
| 40 | + |
| 41 | + ```swift |
| 42 | + extension Sequence { |
| 43 | + func map<T, E>(_ body: (Element) throws(E) -> T) throws(E) -> [T] { ... } |
| 44 | + } |
| 45 | + ``` |
| 46 | + |
| 47 | + When given a non-throwing closure as a parameter, `map` will not throw. |
| 48 | + |
6 | 49 | * [#70065][]:
|
7 | 50 |
|
8 | 51 | With the implementation of [SE-0110][], a closure parameter syntax consisting
|
@@ -9861,6 +9904,7 @@ using the `.dynamicType` member to retrieve the type of an expression should mig
|
9861 | 9904 | [SE-0394]: https://github.com/apple/swift-evolution/blob/main/proposals/0394-swiftpm-expression-macros.md
|
9862 | 9905 | [SE-0397]: https://github.com/apple/swift-evolution/blob/main/proposals/0397-freestanding-declaration-macros.md
|
9863 | 9906 | [SE-0407]: https://github.com/apple/swift-evolution/blob/main/proposals/0407-member-macro-conformances.md
|
| 9907 | +[SE-0413]: https://github.com/apple/swift-evolution/blob/main/proposals/0413-typed-throws.md |
9864 | 9908 | [#64927]: <https://github.com/apple/swift/issues/64927>
|
9865 | 9909 | [#42697]: <https://github.com/apple/swift/issues/42697>
|
9866 | 9910 | [#42728]: <https://github.com/apple/swift/issues/42728>
|
|
0 commit comments