Not so–assignment never changes any object. It just attaches a label to some existing value:
julia> a = [12]; b = a; b = [99]; a
1-element Array{Int64,1}:
12
The fact that the label b
was previously attached to the same value as a
is of no consequence.
Of course, b .= [99]
does mutate a
as well, but that’s not assignment. Instead, it is roughly equivalent to: broadcast!(b, identity, [99])
, which mutates the value to which the label b
is attached. If that value is the same value to which the label a
was attached, then you will see the same modification to a
.
This gets to one of the nicest points about Julia (as opposed to, say, C++), which is that assignments and function arguments always work in the same way for every value. While the compiler may copy values passed into functions, it will only do so for completely immutable values, so there is no practical reason to worry about whether a copy has happened or not. Thus you can write your code without having to worry about whether a value might accidentally be copied. Likewise, there is no need to worry about whether a = b
creates an “alias” or a “copy” depending on the types of a
or b
. All assignments behave in exactly the same way: attaching a label to a value, nothing more.