# Assignment and mutation

**URL:** <https://discourse.julialang.org/t/assignment-and-mutation/19119>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 30, 2018, 4:12pm UTC](https://discourse.julialang.org/t/assignment-and-mutation/19119 "2018-12-30T16:12:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![suiato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suiato/32/1713_2.png) [@suiato](https://discourse.julialang.org/u/suiato)\
**Post date:** [December 30, 2018, 4:12pm UTC](https://discourse.julialang.org/t/assignment-and-mutation/19119/1 "2018-12-30T16:12:10Z")

</div>

Subtle differences between assignment and mutation in Julia as shown in the script below are easy to be missed or misunderstood by a beginner like me. I’m starting to understand and realize the importance, but still not really confident. If there’s a good (clear, plain, accurate and concise) document on the issue for beginners, I really appreciate a pointer to it.

In the script below, temp(a) has an assignment, and temp1(a) a mutation. The difference is just a dot, and the results look the same [4,5,6]. It’s hard for a beginner to realize they are actually different arrays. Interestingly, temp1(a) allocates more than six times of memory than temp(a) and slower (true when @benchmark is used).

temp2(a) is same as temp1(a) except it returns nothing. It has smaller allocation and faster execution than the other two functions.

I was naively using functions like temp(a), but realize that I should use functions like temp2(a) as temp!(a) when I mean mutation of the array a. Is this thinking correct?

```julia
a = [1,2,3];

```

```julia
function temp(a)
    a = [4,5,6]
end
function temp1(a)
    a .= [4,5,6]
end
function temp2(a)
    a .= [4,5,6]
    return nothing
end

```

```
temp2 (generic function with 1 method)

```

```julia
@time a1 = temp(a)

```

```
  0.007462 seconds (3.14 k allocations: 163.691 KiB)

3-element Array{Int64,1}:
 4
 5
 6

```

```julia
@time a2 = temp1(a)

```

```
  0.056520 seconds (20.21 k allocations: 1.006 MiB)

3-element Array{Int64,1}:
 4
 5
 6

```

```julia
a == a1 == a2

```

```
true

```

```julia
a === a1

```

```
false

```

```julia
a === a2

```

```
true

```

```julia
a = [1,2,3]

```

```
3-element Array{Int64,1}:
 1
 2
 3

```

```julia
@time temp2(a)

```

```
  0.005079 seconds (217 allocations: 12.904 KiB)

```

```julia
a

```

```
3-element Array{Int64,1}:
 4
 5
 6

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [December 30, 2018, 4:28pm UTC](https://discourse.julialang.org/t/assignment-and-mutation/19119/2 "2018-12-30T16:28:46Z")

</div>

Maybe this is a good read: [Values vs. Bindings: The Map is Not the Territory · John Myles White](http://www.johnmyleswhite.com/notebook/2014/09/06/values-vs-bindings-the-map-is-not-the-territory/)

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [December 30, 2018, 4:32pm UTC](https://discourse.julialang.org/t/assignment-and-mutation/19119/3 "2018-12-30T16:32:09Z")

</div>

Perhaps this will help as well: [Primitive and Composite? Types - #7 by rdeits](https://discourse.julialang.org/t/primitive-and-composite-types/19076/7)

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 30, 2018, 5:03pm UTC](https://discourse.julialang.org/t/assignment-and-mutation/19119/4 "2018-12-30T17:03:15Z")

</div>

The simplest version of this I can come up with is this:

- Assignment changes which object a name refers to: `x = ex` causes the name `x` to refer to the value resulting from the evaluation of the expression `ex`. Assignment _never_ changes the values of any objects.

- Mutation changes the value of an object: `x[i] = ex` and `x.f = ex` both mutate the object referred to by `x` changing a value at index or a property with a name, respectively. Mutation _never_ changes what objects any names in any scope refer to.

Perhaps the confusion comes from the fact that these all use the `=` in their syntax? They’re really totally unrelated. It’s also possible that people think of assignment as setting a named property on some implicit “scope object”. That’s probably a view that can be worked out coherently, in which case having a clear notion of what all the different “scope objects” are would be crucial but I’m not entirely sure if that’s a helpful way to think about the matter or not.

---

<div class="post-metadata">

**Author:** ![suiato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suiato/32/1713_2.png) [@suiato](https://discourse.julialang.org/u/suiato)\
**Post date:** [December 31, 2018, 9:56am UTC](https://discourse.julialang.org/t/assignment-and-mutation/19119/5 "2018-12-31T09:56:02Z")

</div>

Thanks for the pointer.

I needed to know how to make sure an array in a function was mutating, not copying when I used it thousands of times repeatedly. Because copying increases allocation and tons of garbage collection tend to make it slow.

---

<div class="post-metadata">

**Author:** ![suiato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suiato/32/1713_2.png) [@suiato](https://discourse.julialang.org/u/suiato)\
**Post date:** [December 31, 2018, 9:59am UTC](https://discourse.julialang.org/t/assignment-and-mutation/19119/7 "2018-12-31T09:59:14Z")

</div>

Thank you for a nice and helpful explanation.

The meaning of “=” is indeed ambiguous sometimes, not only in programming languages, but also in mathematics. Even “abused” in a sense…

In R, “\<-” is used for assignment. So, an operation like temp(a ← a + 1) is possible. In Julia, temp(a = a + 1) has a different meaning. In Mathematica, “=:” is used for definition, for example. It uses “-\>”, too. In Matlab,…

I like Julia’s syntax enabling users to write code following math (… there’s a nice word for it…I forgot). It’s quite obvious how important syntax or notation in math. But, I can view the language like Julia not only as following the math, but also extending the math in a sense. The concept of multiple dispatch, for example, is very common in math, but Julia’s extensive, systematic and contextual use of it made it much more general. In a sense, it’s extending math… In that sense, Julia can even clarify and correct ambiguous syntax/notation existing in math for some historical reasons.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 31, 2018, 10:14am UTC](https://discourse.julialang.org/t/assignment-and-mutation/19119/9 "2018-12-31T10:14:27Z")

</div>

> [@suiato](#):
>
> The difference is just a dot

There is a trade-off between keeping the syntax convenient and clearly demarcating various semantics. In a sense, `.=` extends the broadcasting syntax naturally to assignment.

I think you may find it neat when you get used to it, but if you carry over intuition from other languages it is true that you can run into surprises. I would recommend working through the manual (not reference or the developer docs, just the first part).
