|
9 | 9 |
|
10 | 10 | ## Introduction
|
11 | 11 |
|
12 |
| -Swift's error handling model allows functions and closures marked `throws` to note that they can exit by throwing an error. The error values themselve are always type-erased to `any Error`. This approach encourages errors to be handled generically, and remains a good default for most code. However, there are some places where the type erasure is unfortunate, because it doesn't allow for more precise error typing in narrow places where it is possible and desirable to handle all errors, or where the costs of type erasure are prohibitive. |
| 12 | +Swift's error handling model allows functions and closures marked `throws` to note that they can exit by throwing an error. The error values themselves are always type-erased to `any Error`. This approach encourages errors to be handled generically, and remains a good default for most code. However, there are some places where the type erasure is unfortunate, because it doesn't allow for more precise error typing in narrow places where it is possible and desirable to handle all errors, or where the costs of type erasure are prohibitive. |
13 | 13 |
|
14 | 14 | This proposal introduces the ability to specify that functions and closures only throw errors of a particular concrete type.
|
15 | 15 |
|
@@ -222,7 +222,7 @@ Untyped errors have the existential type `any Error`, which incurs some [necessa
|
222 | 222 |
|
223 | 223 | ## Proposed solution
|
224 | 224 |
|
225 |
| -In general we want to add the possibility to use `throws` with a single, specific error type. |
| 225 | +In general, we want to add the possibility of using `throws` with a single, specific error type. |
226 | 226 |
|
227 | 227 | ```swift
|
228 | 228 | func callCat() throws(CatError) -> Cat {
|
@@ -446,9 +446,9 @@ Note that typed throws has elegantly solved our problem, because any throwing si
|
446 | 446 |
|
447 | 447 | ### When to use typed throws
|
448 | 448 |
|
449 |
| -Typed throws makes it possible to strictly specify the thrown error type of a function, but doing so constrains the evolution of that function's implementation. Additionally, errors are usually propagated or rendered, but not exhaustively handled, so even with the addition of typed throws to Swift, untyped `throws` is better for most scenarious. Consider typed throws only in the following circumstances: |
| 449 | +Typed throws makes it possible to strictly specify the thrown error type of a function, but doing so constrains the evolution of that function's implementation. Additionally, errors are usually propagated or rendered, but not exhaustively handled, so even with the addition of typed throws to Swift, untyped `throws` is better for most scenarios. Consider typed throws only in the following circumstances: |
450 | 450 |
|
451 |
| -1. In code that stays within a module or package where you always want to handle the error, so it's a purely an implementation detail and it is plausible to handle the error. |
| 451 | +1. In code that stays within a module or package where you always want to handle the error, so it's purely an implementation detail and it is plausible to handle the error. |
452 | 452 | 2. In generic code that never produces its own errors, but only passes through errors that come from user components. The standard library contains a number of constructs like this, whether they are `rethrows` functions like `map` or are capturing a `Failure` type like in `Task` or `Result`.
|
453 | 453 | 3. In dependency-free code that is meant to be used in a constrained environment (e.g., Embedded Swift) or cannot allocate memory, and will only ever produce its own errors.
|
454 | 454 |
|
@@ -1159,7 +1159,7 @@ Note that one would have to do the same thing with the `rethrows` formulation to
|
1159 | 1159 |
|
1160 | 1160 | ## Effect on ABI stability
|
1161 | 1161 |
|
1162 |
| -The ABI between an function with an untyped throws and one that uses typed throws will be different, so that typed throws can benefit from knowing the precise type. |
| 1162 | +The ABI between a function with an untyped throws and one that uses typed throws will be different, so that typed throws can benefit from knowing the precise type. |
1163 | 1163 |
|
1164 | 1164 | Replacing a `rethrows` function with one that uses typed throws, as proposed for the standard library, is an ABI-breaking change. However, it can be done in a manner that doesn't break ABI by retaining the `rethrows` function only for binary-compatibility purposes. The existing `rethrows` functions will be renamed at the source level (so they don't conflict with the new ones) and made `@usableFromInline internal`, which retains the ABI while making the function invisible to clients of the standard library:
|
1165 | 1165 |
|
|
0 commit comments