# "invalid assignment location" on function call returning view

**URL:** <https://discourse.julialang.org/t/invalid-assignment-location-on-function-call-returning-view/23346>\
**Category:** General Usage\
**Created:** [April 20, 2019, 2:13pm UTC](https://discourse.julialang.org/t/invalid-assignment-location-on-function-call-returning-view/23346 "2019-04-20T14:13:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![grahamas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grahamas/32/3401_2.png) [@grahamas](https://discourse.julialang.org/u/grahamas)\
**Post date:** [April 20, 2019, 2:13pm UTC](https://discourse.julialang.org/t/invalid-assignment-location-on-function-call-returning-view/23346/1 "2019-04-20T14:13:24Z")

</div>

I get an error trying to assign to the `view` result of a function call directly, but not when assigning to that same `view` after saving it in a variable. I can’t figure out why.

```julia
@generated function view_slice_last(arr::Array{T,D}, dx) where {T,D}
    initial_colons = [:(:) for i in 1:(D-1)]
    quote
        @view arr[$(initial_colons...),dx]
    end
end

example = rand(5,4,3)

view_slice_last(example, 2) .*= 0.0 # ERROR: LoadError: Invalid assignment location "view_slice_last(example,2)

saved_view = view_slice_last(example,2)
saved_view .*= 0.0 # works fine!

```

(also if anyone has a better way to do what I did with the generated function please do tell – I imagine there must be something but I couldn’t find anything else)

---

<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:** [April 20, 2019, 2:22pm UTC](https://discourse.julialang.org/t/invalid-assignment-location-on-function-call-returning-view/23346/2 "2019-04-20T14:22:11Z")

</div>

**EDIT** this answer is **incorrect**

Use `()`s, eg

```julia
(view_slice_last(example, 2)) .*= 0.0

```

---

<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:** [April 20, 2019, 2:25pm UTC](https://discourse.julialang.org/t/invalid-assignment-location-on-function-call-returning-view/23346/3 "2019-04-20T14:25:40Z")

</div>

And for an alternative, consider something like

```julia
function view_slice_last2(arr::AbstractArray{T,N}, dx) where {T,N}
    view(arr, ntuple(_ -> Colon(), N - 1)..., dx)
end

```

The Julia 1.2-pre compiler appears to figure everything out nicely, I didn’t test earlier ones.

---

<div class="post-metadata">

**Author:** ![grahamas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grahamas/32/3401_2.png) [@grahamas](https://discourse.julialang.org/u/grahamas)\
**Post date:** [April 20, 2019, 2:32pm UTC](https://discourse.julialang.org/t/invalid-assignment-location-on-function-call-returning-view/23346/4 "2019-04-20T14:32:41Z")

</div>

> [@Tamas\_Papp](#):
>
> ```julia
> (view_slice_last(example, 2)) .*= 0.0
> 
> ```

This doesn’t seem to work, at least in 1.1. Same error.

> [@Tamas\_Papp](#):
>
> ```julia
> function view_slice_last2(arr::AbstractArray{T,N}, dx) where {T,N} 
> view(arr, ntuple(_ -> Colon(), N - 1)..., dx) 
> end
> 
> ```

Great suggestion! The 1.1 compiler figures it out just fine, too.

---

<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:** [April 20, 2019, 3:09pm UTC](https://discourse.julialang.org/t/invalid-assignment-location-on-function-call-returning-view/23346/5 "2019-04-20T15:09:25Z")

</div>

> [@grahamas](#):
>
> This doesn’t seem to work, at least in 1.1. Same error.

You are right, I was thinking of something else. I am not aware of a way that would lower that expression, without an interim variable like you worked around it.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 20, 2019, 4:15pm UTC](https://discourse.julialang.org/t/invalid-assignment-location-on-function-call-returning-view/23346/6 "2019-04-20T16:15:22Z")

</div>

This was fixed in 1.2:

[https://github.com/JuliaLang/julia/issues/31295](https://github.com/JuliaLang/julia/issues/31295)
