Changing the values โโof elements of composite types in an array
In Julia (0.3.0-rc1), when I have an fill
array with composite type instances and update a member of one instance, all the array instances are updated. Is this the intended behavior, and if so, how do I change the value of only one element in the array?
The code in question:
type Foo
x :: Int
y :: Int
end
arr = fill(Foo(2, 4), 3)
arr[2].x = 5
I expect [Foo(2, 4), Foo(5, 4), Foo(2, 4)]
, but I get instead [Foo(5, 4), Foo(5, 4), Foo(5, 4)]
. What am I doing wrong? Should I always update the whole element like in arr[2] = Foo(5, 4)
(which gives the expected results)? TIA.
You have created one instance Foo
and populated an array with references to that one instance.
You will probably need arr = [Foo(2,4) for i in 1:3]
one that will create a new copy Foo(2,4)
for each index.