3 ms·
If iterators are hidden magic, how are the following not hidden magic: for i, v := range someSlice { ... Shouldn't that be eschewed as magic, and instead
by TheDong 2y ago
If iterators are hidden magic, how are the following not hidden magic:
for i, v := range someSlice { ...
Shouldn't that be eschewed as magic, and instead we should write:
for i := 0; i < len(someSlice); i++ { v := someSlice[i]; ...
They both de-sugar to the same thing (well, now they do, it used to re-use the same 'v' in the first one, so it used to be subtly different, but they changed the language's hidden magic).
How is the channel loop not hidden magic?
for v := range channel
// magic for
for {
v, ok := <-channel
if !ok { break }
}
The iterator proposal is adding some sugar which is _less hidden_ than the current 'for range' loops, and people are complaining that's magic?
- 38 2y ago> for i, v := range someSlice { ... what a ridiculous comment - every single programming language has this or similar syntax for iterating a slice.
- TheDong 2y agoAll the languages I can think of that have iteration over a slice also have the ability for users to define iteration over custom collections, such as with generators or whatever... yet the comment I'm replying to is saying custom iterators, something every language has, is too much magic, so clearly "every language has it" isn't enough justification for something not to be magic for them. Anyway, not every language has it. C has arrays, but doesn't have any iteration sugar for them, and the general attitude of gophers does usually seem to be "if C didn't have it, it's not simple, it's magic", and that Go should just be C but with a GC, goroutines, and builtin hashmaps.