For example, numbers are immutable in Julia. If you have x = 3
, that doesn’t mean you can’t change the value of x
! But if you do x = 4
then you are making x
refer to a new value — you aren’t changing the value of 3
! That is:
x = 3
y = x
x = 4
y # still 3!
In contrast, a Vector
is mutable. If you have x = [3,4,5]
, and you do x[1] = 7
, then you are changing the contents of the array in-place:
x = [3,4,5]
y = x # not a copy — y "points" to the same object
x[1] = 7
y # contents are now [7,4,5]
The signature of a mutable object is that if you change it, then other references to the object (via name = object
) “see” the change.
If R.I
is an SVector
in a mutable object R
, then if you do:
y = R.I
R.I = @SVector [3,4,5] # only possible if R is a mutable struct
y # hasn't changed!
whereas if R.I
is a mutable object like a Vector
or an MVector
, then even if R
is a (non-mutable) struct
, you can change the contents of R.I
:
y = R.I
R.I .= @SVector [3,4,5] # .= changes the *contents* of R.I
R.I[1] = 2 # you can also change individual elements
y # prints [2,4,5] — it "sees" the change