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

slices: Equal should call out handling of nil #68472

Closed
dsnet opened this issue Jul 16, 2024 · 5 comments
Closed

slices: Equal should call out handling of nil #68472

dsnet opened this issue Jul 16, 2024 · 5 comments
Labels
Documentation NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@dsnet
Copy link
Member

dsnet commented Jul 16, 2024

Go version

go1.22.0

What did you do?

Read the docs for slices.Equal

What did you see happen?

Saw:

Equal reports whether two slices are equal: the same length and all elements equal. If the lengths are different, Equal returns false. Otherwise, the elements are compared in increasing index order, and the comparison stops at the first unequal pair. Floating point NaNs are not considered equal.

What did you expect to see?

Something like:

Nil and empty slices are considered equal.

Nilness of slices is a more common occurance than NaNs, yet NaNs get called out, but nilness does not. In both cases, the current behavior is implicit with the previous prose, but it's helpful to call out edge cases.

@gabyhelp
Copy link

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@ianlancetaylor
Copy link
Contributor

The doc does explain how nil slices are handled, because it says exactly what the function does. But we can add another sentence if you like.

@dsnet
Copy link
Member Author

dsnet commented Jul 16, 2024

I agree the current wording is correct, but my point is that a little more information (even if strictly redundant) is helpful. After all, we called out the behavior of NaNs, which is technically covered by "all elements equal".

@apparentlymart
Copy link

FWIW, when I read the current text my immediate thought was: that sure sounds like it's implying that empty and nil are treated as equal, but since it doesn't say so explicitly I will read the implementation to check.

I think mentioning it explicitly (rather than just implying it) would be worthwhile given that other parts of the language, and also various third-party libraries, are not consistent in how they handle this difference.

@cherrymui cherrymui added the NeedsFix The path to resolution is known, but the work has not been done. label Jul 17, 2024
@cherrymui cherrymui added this to the Backlog milestone Jul 17, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/599816 mentions this issue: slices: explicitly document nil and empty slice equality

@dmitshur dmitshur modified the milestones: Backlog, Go1.23 Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

7 participants