-
Notifications
You must be signed in to change notification settings - Fork 26
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
Support typed throws #52
Support typed throws #52
Conversation
do { | ||
_ = try res.get() | ||
} catch { | ||
XCTAssertEqual(error, SomeError()) |
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.
Without the change in the Result
extension, this fails to compile with
.../Tests/ConcurrencyExtrasTests/ResultTests.swift:45:24 Cannot convert value of type 'any Error' to expected argument type 'ResultTests.SomeError'
|
||
func testTypedThrows() async throws { | ||
do { | ||
let res = await Result { () async throws(SomeError) -> Int in try await g(0) } |
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.
Unfortunately, this explicit type-pinning of the closure (() async throws(SomeError) -> Int in
) is load bearing. Without it, you still get a Result<Int, any Error>
back. But at least this way the conversion can be expressed and res
is of type Result<Int, SomeError>
.
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.
Thanks for the PR! I think the one thing is we'll want to maintain backwards compatibility with Swift 5.9+ for the moment. Want to #if compiler(>6)
this branch and then fall back to the old logic otherwise?
Will do! |
17dc7a7
to
a30dcd5
Compare
Just checking in case you didn't see the additional commits I pushed - were you happy with those changes? Is there anything I should be updating? |
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.
Thanks!
We've started adopting typed throws in a couple of areas and I noticed that my
Result
extension to handleasync
closures was clashing with the one in this package. The difference is subtle but I think this is a more general way to do this extension.As the test shows, this allows you to convert an
async throws(SomeError)
into aResult<T, SomeError>
. Without this change, the resulting type isResult<T, any Error>
instead.