by poorform · 8y ago · 0 comments
I'm curious what good use-case there is for this, that the language bothered to support and allow this to even compile.
Duplicate variable name declaration + referencing in the same scope is unintuitive at best, and just seems wrong.
The reason why this idiom feels "weird" is that the loop construct of the language really ought to automatically make a fresh instance of the loop counter for each trip through the loop.
for i := 0; i < 3; i++ {
defer func(i int) {
fmt.Println(i)
}(i)
}
This shadows `i` pretty much the same amount as what I wrote. If `i` gets a different name inside the lambda, then it'd also be worse because then you could accidentally use `i` still.