-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Proposal: Result Codable conformance and async init #2741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ktoso
wants to merge
4
commits into
swiftlang:main
Choose a base branch
from
ktoso:patch-8
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+83
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Result: Codable conformance & async init | ||
|
||
* Proposal: SE-NNNN | ||
* Author: [Konrad 'ktoso' Malawski](https://github.com/ktoso) | ||
* Review Manager: TBD | ||
* Status: **Pending implementation** | ||
* Implementation: TBD | ||
|
||
## Introduction | ||
|
||
The `Result` type in Swift is often used to bridge between async and not async contexts, and could use some minor convenience improvements for both `async` contexts as well as encoding/decoding. | ||
|
||
## Motivation | ||
|
||
The Result type is often used to bridge between async and not async contexts, and it may be useful to initialize a `Result` from the result of an async computation, to then pass it along to other non-async code without unwrapping it first. This is currently inconvenitnt because the `init(catching:)` initializer is missing an `async` overload. | ||
|
||
The Result type also is often reached for to encode a success or failure situation. As Swift gained typed throws, it became possible to write a catching initializer using typed throws, that would capture a `Codable` error and this makes it nice to express catching errors which are intended to be encoded as a result. | ||
|
||
Those two changes allow us to write code like this: | ||
|
||
```swift | ||
func accept<A: Codable>(_: A) { ... } | ||
|
||
enum SomeCodableError: Error, Codable { ... } | ||
func compute() async throws(SomeCodableError) -> Int { ... } | ||
|
||
let result: Result<Int, SomeCodableError> = await Result { | ||
try await compute() | ||
} | ||
|
||
accept(result) | ||
``` | ||
|
||
## Detailed design | ||
|
||
We propose two additions to the Result type in the standard library: | ||
|
||
### Async catching initializer | ||
|
||
We propose to add an async "catching" initializer that is equivalent to the existing synchronous version of this initializer: | ||
|
||
```swift | ||
extension Result where Success: ~Copyable { | ||
/// Creates a new result by evaluating a throwing closure, capturing the | ||
/// returned value as a success, or any thrown error as a failure. | ||
/// | ||
/// - Parameter body: A potentially throwing asynchronous closure to evaluate. | ||
@_alwaysEmitIntoClient | ||
public init(catching body: () async throws(Failure) -> Success) async { | ||
do { | ||
self = .success(try await body()) | ||
} catch { | ||
self = .failure(error) | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Conditional `Codable` conformance | ||
|
||
We propose to add a conditional Codable conformance, as follows: | ||
|
||
```swift | ||
extension Result: Codable where Success: Codable, Failure: Codable {} | ||
``` | ||
|
||
The `Codable` implementation is the default, synthesized, one which was defined in [SE-0295: Codable synthesis for enums with associated values](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0295-codable-synthesis-for-enums-with-associated-values.md). | ||
|
||
## Source compatibility | ||
|
||
This proposal is source compatible. Retroactive conformances are already warned about which would be the case for manual Codable conformances declared in adopter codebases. | ||
|
||
## Effect on ABI stability | ||
|
||
The proposal is purely additive. | ||
|
||
The initializer can be backdeployed. | ||
|
||
## Alternatives considered | ||
|
||
### Don't provide these additions | ||
|
||
In practice this means developers have to write their own `Result` types frequently, which is managable but ineffective, especially as the shape and utility of those types is generally a 1:1 copy of the existing `Result` type. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes please!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1