# @views throws an error in combination with a struct

**URL:** <https://discourse.julialang.org/t/views-throws-an-error-in-combination-with-a-struct/77090>\
**Category:** General Usage\
**Tags:** views\
**Created:** [February 25, 2022, 6:48pm UTC](https://discourse.julialang.org/t/views-throws-an-error-in-combination-with-a-struct/77090 "2022-02-25T18:48:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pgoulart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pgoulart/32/17514_2.png) [@pgoulart](https://discourse.julialang.org/u/pgoulart)\
**Post date:** [February 25, 2022, 6:48pm UTC](https://discourse.julialang.org/t/views-throws-an-error-in-combination-with-a-struct/77090/1 "2022-02-25T18:48:50Z")

</div>

I am not sure if this is a bug or just not allowed syntax for some reason. The `@views` macro throws an error under seemingly narrow circumstances as shown in the last line of the example below:

```julia
struct Foo{T}
    a::Vector{T}
end

n = 10
x = Foo(ones(n))
y = ones(n)
z = ones(n)

# These things all work
#-----------------------
#@views is fine on the LHS without 'end'
@views x.a[2:n] .= y[2:n]

#@views is fine on the RHS with 'end'
@views w = x.a[2:end] .+ x.a[2:end]

#@views works on non-structs either way
@views y[2:n] = z[2:n]
@views y[2:end] = z[2:end]

#@views works with a struct like this
@views x.a[2:n] .= y[2:n]
@views x.a[2:end] .= y[2:end]

# This does not work
#------------------------
@views x.a[2:end] .+= y[2:end]

```

The final line above gives me `ERROR: syntax: invalid let syntax`. If this is expected behaviour, what distinguishes the final line from those preceding?

I see this error in both v1.7 and 1.5.

Edit : Opened as issue [44356](https://github.com/JuliaLang/julia/issues/44356). I marked the the post by @goerch as the solution to this because the use of multiple `@view` macros seems a good workaround.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [February 25, 2022, 9:33pm UTC](https://discourse.julialang.org/t/views-throws-an-error-in-combination-with-a-struct/77090/2 "2022-02-25T21:33:48Z")

</div>

> [@pgoulart](#):
>
> ```julia
> # This does not work
> #------------------------
> @views x.a[2:end] .+= y[2:end]
> 
> ```

This seems to work

```julia
(@view x.a[2:end]) .+= (@view y[2:end])

```

I’d call it an omission(bug). Macro programming is hard. Would suspect `.+=` isn’t recognized…

Edit: Hm. Seems to be more complicated, because

```julia
v1 = [1]
v2 = [2]
@views v1[begin:end] .+= v2[begin:end]

```

works. So parser precedence again(had this one time before…)?

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [February 26, 2022, 2:30am UTC](https://discourse.julialang.org/t/views-throws-an-error-in-combination-with-a-struct/77090/3 "2022-02-26T02:30:02Z")

</div>

The issue here is that lowering seems to have problems with let-blocks on the left-hand side of `.+=` expressions:

```julia
julia> let a=[0]; a end .+= 1
ERROR: syntax: invalid let syntax around REPL[5]:1
Stacktrace:
 [1] top-level scope
   @ REPL[5]:1

```

I don’t see why this shouldn’t work, could you open an issue?

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [February 26, 2022, 6:38am UTC](https://discourse.julialang.org/t/views-throws-an-error-in-combination-with-a-struct/77090/4 "2022-02-26T06:38:35Z")

</div>

> [@simeonschaub](#):
>
> The issue here is that lowering seems to have problems with let-blocks on the left-hand side of `.+=` expressions:

Ah, I understand: I should have simply `@macroexpand`-ed this.

---

<div class="post-metadata">

**Author:** ![pgoulart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pgoulart/32/17514_2.png) [@pgoulart](https://discourse.julialang.org/u/pgoulart)\
**Post date:** [February 26, 2022, 10:09am UTC](https://discourse.julialang.org/t/views-throws-an-error-in-combination-with-a-struct/77090/5 "2022-02-26T10:09:08Z")

</div>

Thank you. I have opened an [issue](https://github.com/JuliaLang/julia/issues/44356) with a link back to the discussion here.
