@@ -25,7 +25,7 @@ We offer three variants of `#expect(throws:)`:
25
25
error that compares equal to it; and
26
26
- One that takes a trailing closure and allows test authors to write arbitrary
27
27
validation logic.
28
-
28
+
29
29
The third overload has proven to be somewhat problematic. First, it yields the
30
30
error to its closure as an instance of ` any Error ` , which typically forces the
31
31
developer to cast it before doing any useful comparisons. Second, the test
@@ -61,7 +61,7 @@ that were thrown.
61
61
62
62
## Detailed design
63
63
64
- All overloads of ` #expect(throws:) ` and` #require(throws:)` will be updated to
64
+ All overloads of ` #expect(throws:) ` and ` #require(throws:) ` will be updated to
65
65
return an instance of the error type specified by their arguments, with the
66
66
problematic overloads returning ` any Error ` since more precise type information
67
67
is not statically available. The problematic overloads will also be deprecated:
@@ -151,7 +151,7 @@ let error = try #require(throws: PotatoError.self) {
151
151
152
152
The new code is more concise than the old code and avoids boilerplate casting
153
153
from ` any Error ` .
154
-
154
+
155
155
## Source compatibility
156
156
157
157
In most cases, this change does not affect source compatibility. Swift does not
@@ -217,31 +217,10 @@ N/A
217
217
218
218
## Future directions
219
219
220
- No specific future directions are indicated.
221
-
222
- ## Alternatives considered
223
-
224
- - Leaving the existing implementation and signatures in place. We've had
225
- sufficient feedback about the ergonomics of this API that we want to address
226
- the problem.
227
-
228
- - Having the return type of the macros be ` any Error ` and returning _ any_ error
229
- that was thrown even on mismatch. This would make the ergonomics of the
230
- subsequent test code less optimal because the test author would need to cast
231
- the error to the appropriate type before inspecting it.
232
-
233
- There's a philosophical argument to be made here that if a mismatched error is
234
- thrown, then the test has already failed and is in an inconsistent state, so
235
- we should allow the test to fail rather than return what amounts to "bad
236
- output".
237
-
238
- If the test author wants to inspect any arbitrary thrown error, they can
239
- specify ` (any Error).self ` instead of a concrete error type.
240
-
241
220
- Adopting [ typed throws] ( https://github.com/swiftlang/swift-evolution/blob/main/proposals/0413-typed-throws.md )
242
221
to statically require that the error thrown from test code is of the correct
243
222
type.
244
-
223
+
245
224
If we adopted typed throws in the signatures of these macros, it would force
246
225
adoption of typed throws in the code under test even when it may not be
247
226
appropriate. For example, if we adopted typed throws, the following code would
@@ -258,7 +237,31 @@ No specific future directions are indicated.
258
237
}
259
238
```
260
239
240
+ We believe it may be possible to overload these macros or their expansions so
241
+ that the code sample above _ does_ compile and behave as intended. We intend to
242
+ experiment further with this idea and potentially revisit typed throws support
243
+ in a future proposal.
244
+
245
+ ## Alternatives considered
246
+
247
+ - Leaving the existing implementation and signatures in place. We've had
248
+ sufficient feedback about the ergonomics of this API that we want to address
249
+ the problem.
250
+
251
+ - Having the return type of the macros be ` any Error ` and returning _ any_ error
252
+ that was thrown even on mismatch. This would make the ergonomics of the
253
+ subsequent test code less optimal because the test author would need to cast
254
+ the error to the appropriate type before inspecting it.
255
+
256
+ There's a philosophical argument to be made here that if a mismatched error is
257
+ thrown, then the test has already failed and is in an inconsistent state, so
258
+ we should allow the test to fail rather than return what amounts to "bad
259
+ output".
260
+
261
+ If the test author wants to inspect any arbitrary thrown error, they can
262
+ specify ` (any Error).self ` instead of a concrete error type.
263
+
261
264
## Acknowledgments
262
265
263
- Thanks to the team and to @jakepetroules for starting the discussion that
264
- ultimately led to this proposal.
266
+ Thanks to the team and to [ @jakepetroules ] ( https://github.com/jakepetroules ) for
267
+ starting the discussion that ultimately led to this proposal.
0 commit comments