Skip to content
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

Merged
merged 6 commits into from
Dec 16, 2024

Conversation

finestructure
Copy link
Contributor

We've started adopting typed throws in a couple of areas and I noticed that my Result extension to handle async 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 a Result<T, SomeError>. Without this change, the resulting type is Result<T, any Error> instead.

do {
_ = try res.get()
} catch {
XCTAssertEqual(error, SomeError())
Copy link
Contributor Author

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) }
Copy link
Contributor Author

@finestructure finestructure Dec 8, 2024

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>.

Copy link
Member

@stephencelis stephencelis left a 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?

Sources/ConcurrencyExtras/Result.swift Outdated Show resolved Hide resolved
@finestructure
Copy link
Contributor Author

Want to #if compiler(>6) this branch and then fall back to the old logic otherwise?

Will do!

@finestructure
Copy link
Contributor Author

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?

Copy link
Member

@stephencelis stephencelis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@stephencelis stephencelis merged commit f5d0abc into pointfreeco:main Dec 16, 2024
8 checks passed
@finestructure finestructure deleted the support-typed-throws branch December 17, 2024 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants