Skip to content

Commit 54d5d8f

Browse files
committed
Do not generalize Result.flatMapError yet
1 parent 60a85e7 commit 54d5d8f

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

proposals/0465-nonescapable-stdlib-primitives.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ We generalize `Result` along the same lines as `Optional`, allowing its `success
241241

242242
Important convenience APIs such as `Result.init(catching:)` or `Result.map` will need to require escapability until we introduce a way to formally specify lifetime dependencies. This is unfortunate, but it still enables intrepid Swift developers to experiment with defining interfaces that take (or perhaps even return!) `Result` values.
243243

244-
However, we are already able to generalize a small handful of methods: `get` and the two error-mapping utilities, `mapError` and `flatMapError`.
244+
However, we are already able to generalize a couple of methods: `get` and the error-mapping utility `mapError`.
245245

246246
```swift
247247
func sample<E: Error>(_ res: Result<Span<Int>, E>) -> Int {
@@ -525,7 +525,7 @@ extension Result: Escapable where Success: Escapable & ~Copyable {}
525525
extension Result: Sendable where Success: Sendable & ~Copyable & ~Escapable {}
526526
```
527527

528-
We postpone generalizing most of the higher-order functions that make `Result` convenient to use, as we currently lack the means to reason about lifetime dependencies for such functions. But we are already able to generalize the two functions that do not have complicated lifetime semantics: `mapError` and `flatMapError`.
528+
We postpone generalizing most of the higher-order functions that make `Result` convenient to use, as we currently lack the means to reason about lifetime dependencies for such functions. But we are already able to generalize the one function that does not have complicated lifetime semantics: `mapError`.
529529

530530
```swift
531531
extension Result where Success: ~Copyable & ~Escapable {
@@ -534,16 +534,9 @@ extension Result where Success: ~Copyable & ~Escapable {
534534
_ transform: (Failure) -> NewFailure
535535
) -> Result<Success, NewFailure>
536536
}
537-
538-
extension Result where Success: ~Copyable & ~Escapable {
539-
@_lifetime(copying self) // Illustrative syntax
540-
consuming func flatMapError<NewFailure>(
541-
_ transform: (Failure) -> Result<Success, NewFailure>
542-
) -> Result<Success, NewFailure>
543-
}
544537
```
545538

546-
Both of these functions return a value with the same lifetime as the original `Result` instance.
539+
The returned value has the same lifetime constraints as the original `Result` instance.
547540

548541
We can also generalize the convenient `get()` function, which is roughly equivalent to optional unwrapping:
549542

@@ -888,5 +881,6 @@ Many people contributed to the discussions that led to this proposal. We'd like
888881
- Guillaume Lessard
889882
- John McCall
890883
- Tony Parker
884+
- Ben Rimmington
891885
- Andrew Trick
892886
- Rauhul Varma

0 commit comments

Comments
 (0)