Is it defined behavior to modify `iter` while `for`-looping over it?

If the local i used in the second pass associates to the same object to the i used in the first pass, then they are the same. (my understanding about julia is advanced again☺️)

julia> function test()
           v = Function[]
           for i = S(4)
               push!(v, () -> println("I'm ", i))
               map(x -> x(), v)
           end
       end
test (generic function with 1 method)

julia> test()
I'm [1]
I'm [1, 2]
I'm [1, 2]
I'm [1, 2, 3]
I'm [1, 2, 3]
I'm [1, 2, 3]
I'm [1, 2, 3, 4]
I'm [1, 2, 3, 4]
I'm [1, 2, 3, 4]
I'm [1, 2, 3, 4]

julia> function test()
           v = Function[]
           for i = 1:4
               push!(v, () -> println("I'm ", i))
               map(x -> x(), v)
           end
       end
test (generic function with 1 method)

julia> test()
I'm 1
I'm 1
I'm 2
I'm 1
I'm 2
I'm 3
I'm 1
I'm 2
I'm 3
I'm 4
1 Like