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

Proposal: Go 2: Smooth syntax for non-blocking channel #34203

Closed
nsagnett opened this issue Sep 10, 2019 · 8 comments
Closed

Proposal: Go 2: Smooth syntax for non-blocking channel #34203

nsagnett opened this issue Sep 10, 2019 · 8 comments
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Milestone

Comments

@nsagnett
Copy link

Usually syntax for channel is smooth but only for blocking interaction.

When multiple channels are used in same goroutine, select { case: } syntax is very pretty and useful.

However, when an only one channel is using and we don't want blocking process, we have to write complete syntax with default case, even if it's not required:
select { case foo <- bar: default: }
So, why not integrate new syntax like <~ for non blocking interaction with channels.

@gopherbot gopherbot added this to the Proposal milestone Sep 10, 2019
@mpx
Copy link
Contributor

mpx commented Sep 10, 2019

The Contracts draft design suggests that this functionality could be a library function without requiring specific syntax (if/when Contracts are added).

A hypothetical example:

ok := chans.SendNonBlocking(fooCh, bar)

I think this would be preferable rather than adding various forms of limited custom syntax.

@ianlancetaylor ianlancetaylor added v2 A language change or incompatible library change LanguageChange labels Sep 10, 2019
@ianlancetaylor
Copy link
Contributor

ianlancetaylor commented Sep 10, 2019

Nonblocking reads from channels have their place, but they are not the normal case. It's not clear to me that this is something that comes up often enough to justify adding a new syntax for it. Especially since the new syntax would presumably be as cryptic as <- for new Go users, and the distinction could be confusing.

(Historical note: in the early days of the language a non-blocking receive was written as v, ok := <-c and a non-blocking send was written as ok := c <- v. When we understood the use of closing channels and range and the implications for racy code, we changed v, ok := <-c to report whether the channel was closed, and dropped the non-blocking send. After all, even then, non-blocking operations could be done using select. See the discussion at https://groups.google.com/forum/#!topic/golang-dev/VK_L5x1EgBI).

@ianlancetaylor
Copy link
Contributor

Let me also say that it's essentially impossible to evaluate this proposal without a specific syntax to consider. So you'll need to propose a syntax.

@nsagnett
Copy link
Author

Yes sorry, it was not clear but the suggested syntax will be :
Blocking Send : c <- v
Blocking Read : c := <- v
NonBlocking Send : c <~ v
NonBlocking Read : c := <~ v

@ianlancetaylor
Copy link
Contributor

Thanks. I think that in practice there would need to be some way to know whether the non-blocking send or receive actually sent or received a value.

@nsagnett
Copy link
Author

Yes but I could see this syntax with Fire And Forget philosophy

@ianlancetaylor
Copy link
Contributor

ianlancetaylor commented Sep 11, 2019

A "Fire and Forget philosophy" sounds to me like something that is so special purpose that there is no need to support it with special syntax. We don't need to complicate the language, particularly with a subtle and nearly invisible distinction between <- and <~, for a feature that few programs need.

@nsagnett
Copy link
Author

Thanks for clarification

@golang golang locked and limited conversation to collaborators Sep 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

4 participants