-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: x/exp/future: new package to implement futures #56461
Comments
Sounds like something that can be prototyped outside of the Go project to demonstrate its worth. |
It could indeed. I've created an external package with the proposed implementation. |
I would expect Also, it could be useful to change the signature to |
I think that's unnecessary. Much like a channel, if you want to return an error and another value, just use a struct. Or, if #56462 or something similar got accepted, use that.
I thought of that, but it just seemed like overkill. If you want to use a context, you can just do select {
case <-ctx.Done():
// ...
case <-f.Done():
// ...
} I see no need to complicate the implementation for an easily handled, and probably unlikely, scenario. |
I feel in two minds about this proposal. I think if it's intentionally constrained to be like a "write once, ready many" channel -- no additional "value add" capabilities such as automatic context tracking, error handling, etc -- then it could be a nice utility helper for a relatively common pattern that would hopefully be easy to understand for anyone who is already familiar with channels. What's proposed above does seem to meet that criteria: If this were added then I would expect to use it in a similar way to how I typically use channels today: largely only as an implementation detail inside a library that encapsulates some concurrent work and not exposed prominently in the public API design. But at the same time, that also reduces the value of it being in the standard library: if I won't typically be exposing If this did start to become a significant part of public-facing library APIs then that's where I start to become more concerned about it, because it seems like that would significantly change the "texture" of Go code using this facility: It's pretty rare today for a high-level library (as opposed to low-level synchronization helpers) to start an operation in the background and return an object representing the future result. Instead, library functions typically block and expect their caller to start a new goroutine if the blocking is inconvenient. It would be unfortunate if Go ended up in a similar position as some other ecosystems where there are parallel sets of libraries for "blocking style" vs. "async style" approaches to the same problem. For now then, my conclusion is a conservative "no" vote (also represented in the reactions to the original issue), not because I don't think this would be useful but because I don't think it passes the typical bar for inclusion in the standard library unless we expect it to be broadly used in the public APIs of shared libraries, and the consequences of that concern me. I am curious to see whether and how |
Futures have been proposed previously in various forms, most notably in #17466 where it was turned down due to being too unimportant to be done as a language change. Since then, generics have been added to the language, making it quite simple to implement futures in a type-safe way without needing any language changes. So here's a proposal to do just that.
Conceptually, a future functions similarly to a one-time use channel that can yield the value that it's given more than once. The benefit over just a normal channel is that it simplifies a 1:M case where multiple receivers need to wait on the same piece of data from a single source. This covers some of the cases, for example, that #16620 is meant to handle.
Here's a possible implementation:
And an example of the usage:
This API is patterned after
context.Context
. The exposure of thedone
channel makes it simple to wait for a future in aselect
case, but it's not necessary to use it to get the value in a safe way.If the package was deemed to be useful after sitting in
x/exp
, it could be promoted to eitherx/sync
or directly intosync
.The text was updated successfully, but these errors were encountered: