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

sync/atomic: describe return value of atomic.Bool CompareAndSwap #66095

Open
Lercher opened this issue Mar 4, 2024 · 3 comments
Open

sync/atomic: describe return value of atomic.Bool CompareAndSwap #66095

Lercher opened this issue Mar 4, 2024 · 3 comments
Assignees
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@Lercher
Copy link

Lercher commented Mar 4, 2024

Proposal Details

This is the current documentation for atomic.Bool CompareAndSwap:

// CompareAndSwap executes the compare-and-swap operation for the boolean value x.
func (x *Bool) CompareAndSwap(old, new bool) (swapped bool) {
	return CompareAndSwapUint32(&x.v, b32(old), b32(new))
}

This only happens for the special case Bool: From reading only the types bool * bool -> bool it is not clear if the return value is the first or second parameter, or some other bool value. The documentation comment just states the obvious (CompareAndSwap does compare-and-swap) and swapped is ambiguous IMHO b/c it could mean the swapped-in value new, the swapped-out value old, or that the swap operation was done.

The proposal is to clarify the doc comment as e.g. (assuming this is even correct)

// CompareAndSwap executes the compare-and-swap operation for the boolean value x.
// It returns true if the swap was done.

I'm not native English speaking, so, there is probably better wording.

References

@gopherbot gopherbot added this to the Proposal milestone Mar 4, 2024
@ianlancetaylor
Copy link
Contributor

In this case the name of the result tells you the meaning of the result. This is one of the cases where it is useful to name a result parameter: for documentation.

@ianlancetaylor ianlancetaylor changed the title proposal: atomic: describe return value of atomic.Bool CompareAndSwap sync/atomic: describe return value of atomic.Bool CompareAndSwap Mar 4, 2024
@ianlancetaylor ianlancetaylor added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. and removed Proposal labels Mar 4, 2024
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Mar 4, 2024
@ianlancetaylor ianlancetaylor modified the milestones: Proposal, Backlog Mar 4, 2024
@ianlancetaylor
Copy link
Contributor

There's no reason for this to be a proposal, taking it out of the proposal process.

@gopherbot
Copy link

Change https://go.dev/cl/572179 mentions this issue: sync/atomic: add clarifying sentence to comment for *Bool.CompareAndSwap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
Development

No branches or pull requests

5 participants