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

Return values #103

Merged
merged 5 commits into from
Nov 8, 2023
Merged

Return values #103

merged 5 commits into from
Nov 8, 2023

Conversation

chriso
Copy link
Contributor

@chriso chriso commented Nov 8, 2023

This PR adds optional support for coroutine return values.

If the coroutine was constructed with a new coroutine.NewWithReturn(func () R) constructor, the return value can be retrieved using:

for c.Next() {
   // handle yields
}
var result R = c.Result()

The reason we haven't added support for passing arguments to coroutines is because it's easy to pass in data using a closure. On the other hand, getting data out of coroutines has so far been problematic. Although you can create a final yield point to pass return values out of the coroutine, the issue in #84 arises.

This PR provides a native way to get data out of a coroutine, so that you don't have to manually create a final yield point.

To keep things simple, I chose to reuse the yield type R rather than introduce a new generic type parameter for the return value.

Copy link
Contributor

@achille-roussel achille-roussel left a comment

Choose a reason for hiding this comment

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

🚢

@chriso chriso merged commit 5b44969 into main Nov 8, 2023
2 checks passed
@chriso chriso deleted the return-values branch November 8, 2023 23:14
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