# Assignment to multiple targets - how does it work?

**URL:** https://discourse.julialang.org/t/assignment-to-multiple-targets-how-does-it-work/11721
**Category:** General Usage
**Tags:** question, indexing, broadcast, array
**Created:** [June 16, 2018, 6:08pm UTC](https://discourse.julialang.org/t/assignment-to-multiple-targets-how-does-it-work/11721 "2018-06-16T18:08:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [June 16, 2018, 6:08pm UTC](https://discourse.julialang.org/t/assignment-to-multiple-targets-how-does-it-work/11721/1 "2018-06-16T18:08:20Z")

</div>

Given:

```julia
julia> a = zeros(5); b = zeros(5); R = collect(10:10:100);

```

This naturally fails:

```julia
julia> a[1:end] = R
ERROR: DimensionMismatch("tried to assign 10 elements to 5 destinations")
Stacktrace:
 [1] throw_setindex_mismatch(::Array{Float64,1}, ::Tuple{Int64}) at .\indices.jl:92
 [2] setindex_shape_check(::Array{Float64,1}, ::Int64) at .\indices.jl:143
 [3] setindex!(::Array{Float64,1}, ::Array{Float64,1}, ::UnitRange{Int64}) at .\array.jl:618

```

But then this succeeds:

```julia
julia> c, d = R;

julia> c
10

julia> d
20

```

Why doesn’t this fail with a “tried to assign 10 elements to 2 destinations” error? Why does Julia silently discard the rest of the values in such cases?

Also:

```julia
julia> a[1:end],b = R;

julia> (a,)
([10.0, 10.0, 10.0, 10.0, 10.0],)

julia> (b,)
(20,)

```

This is seen as a broadcast of R’s first value into `a` (and gives an appropriate warning in 0.7), whereas:

```julia
julia> m, n, o, p, q, b = R;

julia> (m, n, o, p, q)
(10, 20, 30, 40, 50)

julia> (b,)
(60,)

```

distributes R’s values over the variables.

What will be the semantics of the previous `a[1:end],b = R;` assignment after the deprecation is removed?

And is there an easy way to get the effect of `a[1], a[2], a[3], a[4], a[5], b = R;` with more compact and more generalizable code?

---

<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: [June 17, 2018, 6:00am UTC](https://discourse.julialang.org/t/assignment-to-multiple-targets-how-does-it-work/11721/2 "2018-06-17T06:00:47Z")

</div>

There are two things going on here.

First, [multiple return values are destructured](https://docs.julialang.org/en/latest/manual/functions/#Multiple-Return-Values-1). Whenever you have a comma-separated list of variable names on the left hand side of a `=`, that is treated as a tuple and the iterable on the right hand side is assigned elementwise. Extra values are not.

The other expression is an invocation of `setindex!`, eg

```julia
julia> Meta.lower(Base, :(a[1:end] = R))
:($(Expr(:thunk, CodeInfo(
 1 ─ %1 = Base.lastindex(:a)::Any │
 │ %2 = :(:)(1, %1)::Any │
 │ Base.setindex!(:a, :R, %2) │
 └── return :R │
))))

```

`setindex!` checks for conforming sizes, hence the `DimensionMismatch`.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 5, 2021, 12:11pm UTC](https://discourse.julialang.org/t/assignment-to-multiple-targets-how-does-it-work/11721/3 "2021-08-05T12:11:10Z")

</div>

> [@Tamas\_Papp](#):
>
> First, [multiple return values are destructured](https://docs.julialang.org/en/latest/manual/functions/#Multiple-Return-Values-1).

We often find broken links in old posts like the one above. This should probably be pointing to the manual [here](https://docs.julialang.org/en/v1/manual/functions/#Multiple-Return-Values).

_ **NB:** _  
_Could the admins automatically flag all broken links to the respective post authors?_

---

<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: [August 5, 2021, 12:19pm UTC](https://discourse.julialang.org/t/assignment-to-multiple-targets-how-does-it-work/11721/4 "2021-08-05T12:19:15Z")

</div>

Personally, I would prefer not to be notified or pinged about broken links in posts I made years ago.

Broken links are an inevitable part of the web. Readers can just use search engines to figure out the current URL.
