4 ms·
I wish Go also had this: for v := range vs {} instead of this: for _, v := range vs {} How often do you need to have both the element and its index? I'
by kgeist 3y ago
I wish Go also had this:
for v := range vs {}
instead of this:
for _, v := range vs {}
How often do you need to have both the element and its index? I'd say it's not the majority of cases. And now you have to type "_," in 99% loops.
That would be a far more serious breaking change, however. Although, that would only in practice affect integer lists (i.e. code silently compiles). For other types, the compiler would complain about type mismatch errors and refuse to compile (which is better than nothing).
- tapirl 3y agoI have the same feeling as you. Go indeed has for v := range vs {} But unfortunately here v means element index.
- masklinn 3y ago> How often do you need to have both the element and its index? I'd say it's not the majority of cases. And now you have to type "_," in 99% loops. Needing either the index or both is likely a lot more common in Go than in other languages owing to the lack of any sort of iterator or composition. So e.g. any sort of zipping or in-place mapping requires the index. > Although, that would only in practice affect integer lists (i.e. code silently compiles). Not so. It would also affect strings (runes are integers), as well as maps with the same key and value types. Plus it could affect every other case depending how the item is used.
- kgeist 3y ago>Needing either the index or both is likely a lot more common in Go than in other languages owing to the lack of any sort of iterator or composition In our codebase, there's very few instances of needing the index. Usually you just want to iterate over a list and apply some function(s) to the elements or add them to another list.