# Dot-assignment ... what is going on here?

**URL:** https://discourse.julialang.org/t/dot-assignment-what-is-going-on-here/2579
**Category:** General Usage
**Tags:** question
**Created:** [March 9, 2017, 11:06pm UTC](https://discourse.julialang.org/t/dot-assignment-what-is-going-on-here/2579 "2017-03-09T23:06:08Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [March 9, 2017, 11:06pm UTC](https://discourse.julialang.org/t/dot-assignment-what-is-going-on-here/2579/1 "2017-03-09T23:06:08Z")

</div>

Run the following example:

```julia
x = zeros(2)
y = zeros(3)
[x;y] .= [1.2, 4.5, 2.3, 4.5, 5.6]

```

Surprisingly (for me at least), `x,y` are still zero after this. Why?

I am using v0.5.0.

---

<div class="post-metadata">

### Author: ![malmaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malmaud/32/29_2.png) [@malmaud](https://discourse.julialang.org/u/malmaud)
#### Post date: [March 9, 2017, 11:18pm UTC](https://discourse.julialang.org/t/dot-assignment-what-is-going-on-here/2579/2 "2017-03-09T23:18:09Z")

</div>

`[x;y`] constructs a new array that copies the content of `x` and `y` but has no connection to `x` and `y` after being constructed. You are assigning to that new array, not the original `x` and `y`.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [March 9, 2017, 11:24pm UTC](https://discourse.julialang.org/t/dot-assignment-what-is-going-on-here/2579/3 "2017-03-09T23:24:30Z")

</div>

Often the best way to examine these sorts of issues is by asking Julia itself. The `expand` function removes any “syntactic sugar” and displays the code that Julia ends up running. In this case:

```julia
julia> expand(:([x;y] .= [1.2, 4.5, 2.3, 4.5, 5.6]))
:((Base.broadcast!)(Base.identity,vcat(x,y),(Base.vect)(1.2,4.5,2.3,4.5,5.6)))

```

That is, it’s broadcasting the `identity` function over your array, and then storing the answer into the result of `vcat(x,y)`. It’s the result of that `vcat` operation that gets mutated… but you don’t have a reference to it, so you never see it happen.

---

<div class="post-metadata">

### Author: ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)
#### Post date: [March 10, 2017, 12:41am UTC](https://discourse.julialang.org/t/dot-assignment-what-is-going-on-here/2579/4 "2017-03-10T00:41:52Z")

</div>

I feel like we may want to syntactically disallow this, unless anybody can think of a useful purpose for this syntax.

---

<div class="post-metadata">

### Author: ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)
#### Post date: [March 10, 2017, 12:55am UTC](https://discourse.julialang.org/t/dot-assignment-what-is-going-on-here/2579/5 "2017-03-10T00:55:06Z")

</div>

It’s hard for the parser to know which functions return a view and which create a new object.

I have recently used `view(...) .= ...` and the result was fast to execute, while being expressive and easy to read.

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [March 10, 2017, 1:22pm UTC](https://discourse.julialang.org/t/dot-assignment-what-is-going-on-here/2579/6 "2017-03-10T13:22:04Z")

</div>

@andyferris But `view([x;y]) .= [1.2, 4.5, 2.3, 5.6, 2.3]` still doesn’t work (it gives an error).

Maybe one could conceive a generalization of `view`. Currently, `view` only works to return a _subarray_, where it transparently maps indexes on the view to indices on the original array. It could be just as useful to have a function to map indexes to a _super array_, like a block matrix. For example, suppose I want to work with the block matrix:

```julia
M = [A11 A12; A21 A22]

```

without having to instantiate it (maybe the `A`’s are too large). I would like a transparent way of refering to the index `M[i,j]`, where it automatically maps this index to the appropriate index in the appropriate `A`.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [March 10, 2017, 1:23pm UTC](https://discourse.julialang.org/t/dot-assignment-what-is-going-on-here/2579/7 "2017-03-10T13:23:07Z")

</div>

See [CatViews.jl](https://github.com/ahwillia/CatViews.jl).

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [March 10, 2017, 1:54pm UTC](https://discourse.julialang.org/t/dot-assignment-what-is-going-on-here/2579/8 "2017-03-10T13:54:49Z")

</div>

@ChrisRackauckas Nice!

```julia
using CatViews
x = zeros(2)
y = zeros(3)
CatView(x,y) .= [1.2, 4.5, 2.3, 4.5, 5.6]

```

works:

```julia
julia> [x;y]
5-element Array{Float64,1}:
 1.2
 4.5
 2.3
 4.5
 5.6

```

---

<div class="post-metadata">

### Author: ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)
#### Post date: [March 10, 2017, 10:39pm UTC](https://discourse.julialang.org/t/dot-assignment-what-is-going-on-here/2579/9 "2017-03-10T22:39:13Z")

</div>

No, I meant use `view` instead of `getindex`.

E.g. using the `@view` macro,

```julia
@view(a[:, :, 3]) .+= myvector

```

---

<div class="post-metadata">

### Author: ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)
#### Post date: [March 10, 2017, 10:39pm UTC](https://discourse.julialang.org/t/dot-assignment-what-is-going-on-here/2579/10 "2017-03-10T22:39:49Z")

</div>

Nice, @ChrisRackauckas

---

<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: [March 13, 2017, 2:19pm UTC](https://discourse.julialang.org/t/dot-assignment-what-is-going-on-here/2579/11 "2017-03-13T14:19:04Z")

</div>

The only useful meaning I could see this having writing data in place to the two arrays, which would actually be pretty cool, but I’m not sure how reasonably we can add that functionality to Base Julia (it can be done via CatViews as demonstrated). It might make sense to disallow the syntax until it can be supported and then re-allow it.

---

<div class="post-metadata">

### Author: ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)
#### Post date: [March 16, 2017, 5:57am UTC](https://discourse.julialang.org/t/dot-assignment-what-is-going-on-here/2579/12 "2017-03-16T05:57:47Z")

</div>

Note that this is a bug; see e.g. [https://github.com/JuliaLang/julia/issues/20623](https://github.com/JuliaLang/julia/issues/20623).

---

<div class="post-metadata">

### Author: ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)
#### Post date: [December 4, 2018, 6:16pm UTC](https://discourse.julialang.org/t/dot-assignment-what-is-going-on-here/2579/13 "2018-12-04T18:16:23Z")

</div>

@mbauman: is there an equivalent to `expand` in Julia v1?

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [December 4, 2018, 6:25pm UTC](https://discourse.julialang.org/t/dot-assignment-what-is-going-on-here/2579/14 "2018-12-04T18:25:20Z")

</div>

Yes, it’s now called `Meta.@lower` and you no longer need to worry about quoting. The internal machinations of broadcast have changed, too, but the end result in this context is the same.

```julia
julia> Meta.@lower [x;y] .= [1.2, 4.5, 2.3, 4.5, 5.6]
:($(Expr(:thunk, CodeInfo(
1 ─ %1 = (Base.vcat)(x, y)
│ %2 = (Base.vect)(1.2, 4.5, 2.3, 4.5, 5.6)
│ %3 = (Base.broadcasted)(Base.identity, %2)
│ %4 = (Base.materialize!)(%1, %3)
└── return %4
))))

```

---

<div class="post-metadata">

### Author: ![juthohaegeman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juthohaegeman/32/8620_2.png) [@juthohaegeman](https://discourse.julialang.org/u/juthohaegeman)
#### Post date: [March 7, 2019, 10:46pm UTC](https://discourse.julialang.org/t/dot-assignment-what-is-going-on-here/2579/15 "2019-03-07T22:46:43Z")

</div>

I am still a bit confused by how dot assignment is lowered:

```julia
a = randn(3)
b = randn(3)
view(a,:) .= view(b,:) # this works
a .+= b # this works
view(a,:) .+= view(b,:) # this does not work

```

Unfortunately, `Meta.@lower` doesn’t make me any wiser:

```julia
julia> Meta.@lower a .+= b
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope'
1 ─ %1 = Base.broadcasted(+, a, b)
│ %2 = Base.materialize!(a, %1)
└── return %2
))))

julia> Meta.@lower view(a,:) .= view(b,:)
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope'
1 ─ %1 = view(a, :)
│ %2 = view(b, :)
│ %3 = Base.broadcasted(Base.identity, %2)
│ %4 = Base.materialize!(%1, %3)
└── return %4
))))

julia> Meta.@lower view(a,:) .+= view(b,:)
:($(Expr(:error, "invalid assignment location \"view(a, :)\"")))

```

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [March 7, 2019, 10:52pm UTC](https://discourse.julialang.org/t/dot-assignment-what-is-going-on-here/2579/16 "2019-03-07T22:52:45Z")

</div>

That’s a false-positive from the parser — I’d say that’s a bug. Mind filing an issue?

In this particular case, you can use `@views a[:] .+= b[:]` to work around it (if this is indeed the form of your original problem that led you here).

---

<div class="post-metadata">

### Author: ![juthohaegeman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juthohaegeman/32/8620_2.png) [@juthohaegeman](https://discourse.julialang.org/u/juthohaegeman)
#### Post date: [March 8, 2019, 7:26am UTC](https://discourse.julialang.org/t/dot-assignment-what-is-going-on-here/2579/17 "2019-03-08T07:26:00Z")

</div>

Thanks for the response; issue filed here: [https://github.com/JuliaLang/julia/issues/31295](https://github.com/JuliaLang/julia/issues/31295)
