I have a beginner question for which I cannot find the answer after quite 
some searching. 


type MyType
  a::Array{Float64,1}
end

# Fill an array with "independent" instances of MyType. (By the way, is 
there a shorter way to do this?)

MyType_Vec = Array(MyType, 3);

for i in eachindex(MyType_Vec)

  MyType_Vec[i] = MyType([0.0]);
 
end

MyType_Vec

# Change the value of MyType_Vec[1].a[1] to 2.

MyType_Vec[1].a[1] = 2;

MyType_Vec

# Shuffle the vector but with repeated elements

MyType_Vec = MyType_Vec[[1,3,1]];

# Change the value of MyType_Vec[1].a[1] to 4.

MyType_Vec[1].a[1] = 4;

MyType_Vec

# The value of MyType_Vec[3].a[1] also changed to 4.


How can I avoid this reference behaviour? I want that, after the shuffling 
step, the 3 elements of MyType_Vec should be "independent" so that if I 
change MyType_Vec[1].a[1] nothing else is affected. I have tried copy and 
deepcopy without success.

I would be very happy if someone could help me out. I'm stuck...

Many thanks in advance.

Reply via email to