Indexing (relatively) large nested list causes unintended mutation; doesn't occur after deepcopy

On each line of the above, ans is changing to the result of the previous line as demonstrated below.

julia> a = [[1,2,3,],[4,5,6]]
2-element Vector{Vector{Int64}}:
 [1, 2, 3]
 [4, 5, 6]

julia> ans[1]
3-element Vector{Int64}:
 1
 2
 3

julia> ans[1]
1

julia> ans[1]
1

This is equivalent to if you have done the following:

julia> a = [[1,2,3,],[4,5,6]]
2-element Vector{Vector{Int64}}:
 [1, 2, 3]
 [4, 5, 6]

julia> b = a[1]
3-element Vector{Int64}:
 1
 2
 3

julia> c = b[1]
1

julia> d = c[1]
1

This is because ans is a special variable in the REPL. The value of the previously evaluated line is assigned to ans. Do not use ans as a variable name.

help?> ans
search: ans transpose transcode contains expanduser instances MathConstants

  ans

  A variable referring to the last computed value, automatically set at the
  interactive prompt.
3 Likes