-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
proposal: slices: add AllPointer #69345
Comments
Related Issues and Documentation (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
|
I don't think using "the wrong index when writing nested loops" is much of a problem. Looping with an index variable is such a common activity in every programming language. |
Agree that "the wrong index when writing nested loops" should not be the main reason for this proposal. |
Writing |
In an ideal world, Barring that, it becomes a question of choosing between the remaining mediocre options. Emoji voting isn't in favor of this particular one, so I'll close this now. For future reference, another mediocre option that already exists in the language is to shadow the index variable: for p := range s {
p := &s[p]
// ...
} |
Oh, I would like this as well (or something like #69337 or #21537). I thought that changes in for loop range iteration in 1.22 will make this easier to add, but it seems from #69337 Golang maintainers are still not for it? It is really one of very common pain points (even if the solution is easy) . I think what @griesemer wrote here still stands, the fact that this asymmetry exists is bothersome. We got rid of one reason to write My main motivation for this is that I would like to be changing elements in a slice while I am iterating over the slice. Currently I have to reassign at the end of the loop to the slice (like |
This issue was closed by the author, it was not declined by the proposal team. |
For closing by the team I meant #69337. This is why I am writing here to the author to maybe reopen this, if this might be more to their liking. |
Proposal Details
Currently, ranging over slices and arrays only provides read access since the elements are copied by value. Adding a way to modify them when using range would enable loops to omit using index variables entirely for most cases, apart from the rare cases when index arithmetic is actually necessary.
This would, among other things, prevent accidentally using the wrong index when writing nested loops.
Implementation
The text was updated successfully, but these errors were encountered: