# Error with @views and a function

**URL:** <https://discourse.julialang.org/t/error-with-views-and-a-function/102284>\
**Category:** General Usage\
**Tags:** arrays, views\
**Created:** [July 31, 2023, 3:20am UTC](https://discourse.julialang.org/t/error-with-views-and-a-function/102284 "2023-07-31T03:20:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [July 31, 2023, 3:20am UTC](https://discourse.julialang.org/t/error-with-views-and-a-function/102284/1 "2023-07-31T03:20:24Z")

</div>

I’m getting this strange error when trying to use views with a function. Here is a MWE:

```julia
x = Array{Float64}(undef, 3, 3, 3)
y = similar(x)

@views function fun(x, y, t)
    y[:, :, t], x[:, :, t] = rand(3, 3)
    return nothing
end

```

When I try to define `fun` I get the error

```julia
ERROR: syntax: function argument name not unique: ":" around REPL[3]:2
Stacktrace:
 [1] top-level scope
   @ REPL[3]:1

```

Why does that happen?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 31, 2023, 5:05am UTC](https://discourse.julialang.org/t/error-with-views-and-a-function/102284/2 "2023-07-31T05:05:35Z")

</div>

First we have to address that without the `@view` macro, that code would cause another error:

```julia
julia> fun(x, y, 1)
ERROR: ArgumentError: indexed assignment with a single value to possibly many locations is not supported; perhaps use broadcasting `.=` instead?

```

`y[:, :, t], x[:, :, t] = rand(3, 3)` is a problem, you can’t assign to multiple destinations in one line. `@views` does not account for this, and it blindly transformed the error-throwing code to something that throws another error. You can check `@macroexpand` to see what it does:

```julia
julia> @macroexpand @views function fun(x, y, t)
           y[:, :, t], x[:, :, t] = rand(3, 3)
           return nothing
       end
:(function fun(x, y, t)
      #= REPL[4]:1 =#
      #= REPL[4]:2 =#
      ((Base.maybeview)(y, :, :, t), (Base.maybeview)(x, :, :, t)) = rand(3, 3)
      #= REPL[4]:3 =#
      return nothing
  end)

```

The parser mistakes that as a method definition, one with 2 identical arguments `:`.

This fix works fine, but `@views` seems unnecessary:

```julia
@views function fun(x, y, t)
  result = rand(3,3)
  y[:, :, t] = result
  x[:, :, t] = result
  return nothing
end

```

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [July 31, 2023, 5:12am UTC](https://discourse.julialang.org/t/error-with-views-and-a-function/102284/3 "2023-07-31T05:12:29Z")

</div>

Oh, that multiple assignments was a mistake copying the example here.  
In my application the assignment is something like

```julia
y[:, :, t], x[:, :, t] = otherfun(...)

```

where `otherfun` returns two different arrays that are conformable with `x[:, :, t]` and `y[:, :, t]`.  
I read somewhere a long time ago that `@views` can save some time because slicing arrays create temporary copies but views do not. Is that still a valid point?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 31, 2023, 6:08am UTC](https://discourse.julialang.org/t/error-with-views-and-a-function/102284/4 "2023-07-31T06:08:15Z")

</div>

> [@amrods](#):
>
> where `otherfun` returns two different arrays

Yeah that should work without error. Still, `@views` doesn’t transform it properly because it assumes single assignment.

> [@amrods](#):
>
> I read somewhere a long time ago that `@views` can save some time because slicing arrays create temporary copies but views do not. Is that still a valid point?

That’s true, but you don’t allocate here to begin with because the slicing is on the left side of the assignment. Allocations happen on the right side, which includes expressions like `A[i] += B` because that just lowers to `A[i] = A[i] + B`.
