Skip to content

Clarify forever channel declarations #316

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

Merged
merged 1 commit into from
Mar 20, 2022
Merged

Clarify forever channel declarations #316

merged 1 commit into from
Mar 20, 2022

Conversation

SimonAlling
Copy link
Contributor

@SimonAlling SimonAlling commented Mar 9, 2022

The current idiom

forever := make(chan bool)

gives the impression that booleans will be sent through forever. But
the channel never has anything sent through it; its purpose is just to
block indefinitely.

This PR clarifies the idiom in two ways:

  • bool is replaced by struct{} – the unit type, as Go has no
    bottom type (empty type; ⊥) – clarifying that whatever may be sent
    through the channel will never carry any information.
  • The channel itself is replaced by nil – which always blocks when
    read from or written to (see below) – clarifying that nothing at all
    will ever be sent through it, leaving no doubts as to its purpose.

From the specification:

  • "A nil channel is never ready for communication."
  • "Receiving from a nil channel blocks forever."
  • "A send on a nil channel blocks forever."

The current idiom

    forever := make(chan bool)

gives the impression that booleans will be sent through `forever`.  But
the channel never has anything sent through it; its purpose is just to
block indefinitely.

Go doesn't have any bottom type (empty type; ⊥), but it does have a unit
type, namely `struct{}`.  Because not even such values will be sent, the
channel need not even exist at all, leaving no doubts as to its purpose.
@SimonAlling
Copy link
Contributor Author

I'm not experienced enough with Go to know for sure that this is functionally equivalent, so feedback is appreciated.

@SimonAlling
Copy link
Contributor Author

I have clarified the PR description; GitHub typically doesn't update the resulting squash commit message accordingly, so here it is:

The current idiom

    forever := make(chan bool)

gives the impression that booleans will be sent through `forever`.  But
the channel never has anything sent through it; its purpose is just to
block indefinitely.

This PR clarifies the idiom in two ways:

  * `bool` is replaced by `struct{}` – the unit type, as Go has no
    bottom type (empty type; ⊥) – clarifying that whatever may be sent
    through the channel will never carry any information.
  * The channel itself is replaced by `nil` – which always blocks when
    read from or written to (see below) – clarifying that nothing at all
    will ever be sent through it, leaving no doubts as to its purpose.

From [the specification](https://go.dev/ref/spec):

  * "A `nil` channel is never ready for communication."
  * "Receiving from a `nil` channel blocks forever."
  * "A send on a `nil` channel blocks forever."

Copy link
Contributor

@michaelklishin michaelklishin left a comment

Choose a reason for hiding this comment

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

Your reasoning makes sense to me. Thank you for contributing!

@michaelklishin michaelklishin merged commit 061f591 into rabbitmq:master Mar 20, 2022
@SimonAlling SimonAlling deleted the clarify-forever-channels branch March 23, 2022 14:10
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