-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
spec: clarify that map iteration always yields the latest version of an entry #70916
Comments
Related Issues
Related Documentation Related Discussions (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
Same as/for slices? |
I think for slices/arrays it's clearer: the produced index values |
Not sure this needs to be a proposal. |
Change https://go.dev/cl/641555 mentions this issue: |
[From the CL discussion] In retrospect, after starring at the spec some more, I think we don't really need this: arguably we have the same problem with slice iteration. There we say that the indices are produced from 0 to n-1, and then we say (in the table) that the corresponding value is a[i]. Slice elements act like variables (they are addressable), and in the section on variables we say "A variable's value is retrieved by referring to the variable in an expression; it is the most recent value assigned to the variable." What "most recent" means in detail is left out and for that we need the Go Memory Model. For maps, we also say (via the table) that the (2nd) value is m[k] which should be sufficient for an implementation to behave as we'd expect. But the values of map entries don't act like variables (they are not addressable), so that phrase for variables doesn't apply. Maybe in the section on (map) index expressions we should say something like: "if the map contains an entry with key x, a[x] is the most recently stored map element with key x". |
Change https://go.dev/cl/641856 mentions this issue: |
Proposal Details
Consider the following program (https://go.dev/play/p/wd1Ge2PIyAK):
Map iteration order unspecified, so if "a" is produced first it will print:
If "b" is produced first, the spec does not specify whether this must print
Or if
is acceptable. In other words, is iteration required to produce the latest version of an entry, or is a stale version OK?
As far as I know, the
gc
map implementation has always produced the latest version of an entry. In fact, when implementing #54766 I fixed bugs I introduced that produced stale values under the assumption that the spec required it.The spec does specify "If a map entry that has not yet been reached is removed during iteration, the corresponding iteration value will not be produced." In my opinion it would be odd to require deletion be tracked precisely, but not changes to values.
Thus I propose that the map portion of https://go.dev/ref/spec#For_range be amended to add "Iteration always produces the latest value of a map entry."
cc @griesemer @randall77
The text was updated successfully, but these errors were encountered: