Why does in-place array assignment behave differently here?

Hm, that’s interesting. I would suggest investigating what happens to that array, for example @showing it before and after swapping.
Note that if you change the contents of the array w2 in f the actual array changes as well, just like

julia> a = [1, 2]
2-element Array{Int64,1}:
 1
 2

julia> b = a
2-element Array{Int64,1}:
 1
 2

julia> b[1] = 3
3

julia> a
2-element Array{Int64,1}:
 3
 2

Maybe that’s what’s happening?

Besides all that, what is the behavior which you want? If it’s the copy-version, you can just put the swap outside the loop and you should have no problems.

1 Like