On 17 October 2016 at 17:18, Sokolov Yura <funny.fal...@gmail.com> wrote:
> понедельник, 17 октября 2016 г., 18:12:59 UTC+3 пользователь Roberto Zanotto
>> I missed the chance to comment on github...
>> This also works for implementing futures and it's simple, type-safe and
>> can be used with select:
>> Of course the syntax is not as nice as built-in futures.
> Roberto, no it doesn't work.
> You miss possibility of filling future from several (usually, two)
> concurrently running goroutines.
I'd suggest that this requirement is fairly unusual. For example,
I just glanced at the C++ future implementation which suggests
using std::promise::set_value to set the associated value, and that
raises an exception if you try to set the value twice.
If you want to make it possible, it's pretty easy:
There's another idiom I quite like for futures (when there's only one
possible source of the value) putting the value back after
taking it out: https://play.golang.org/p/_7p69KE_RZ
Quite a while ago I wrote a blog post that leveraged that idiom to
make a kind of broadcast channel: https://rogpeppe.wordpress.com/2009/12/
That technique is also applicable to having multiple values in a channel
to implement a pool of available tokens.
BTW here's an example of a Go package that uses the future idiom
quite a bit: https://godoc.org/github.com/hashicorp/raft#ApplyFuture
It doesn't seem so onerous to do something like this without
needing direct support in the language (it would be easy
to add a wait channel too).
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups
To unsubscribe from this group and stop receiving emails from it, send an email
For more options, visit https://groups.google.com/d/optout.