@views bug or expected behaviour?

Consider the code

n = 5
z = randn(ComplexF64, n)
x = zeros(n)
y = zeros(n)
i = 2
j = 4
@views x[i:j] , y[i:j] = reim(z[i:j])

In julia 1.6.1 this throws the error

ERROR: syntax: "i:j" is not a valid function argument name around REPL[7]:1

Removing the indexing on the LHS gets rid of the error. i.e. the following works:

@views xx,yy = reim(z[i:j])

This seems like a bug to me but before posting an issue I thought I’d ask about it here in case I’m missing the reason why I shouldn’t expect it to work. So, is this a bug, and if not why doesn’t it work?

Thanks, Patrick

The error message is pretty unhelpful, I agree, but I think @views is doing what you’d expect here. If you expand out the expected result of @views, you get:

julia> view(x, i:j), view(y, i:j) = reim(view(z, i:j))
ERROR: syntax: "i:j" is not a valid function argument name around REPL[21]:1
Stacktrace:
 [1] top-level scope
   @ REPL[21]:1

which also doesn’t work.

This doesn’t really have anything to do with views: It’s just the fact that you can’t use a function call as the left-hand side of an = assignment in Julia.

Well, actually, you can do that, but that syntax already means something else:

view(x, i) = ...  # this *defines* a new function named `view`

so when you try to do view(x, ...), view(y, ...) = ... you don’t get the result you’re hoping for.

The good news is that you don’t need to put the view annotation on the left-hand side anyway.

1 Like

tldr:


Basically, it doesn’t make sense to do a non-view on the left-hand side when you’re assigning values into an array – what would that even mean? you copied a slice and you assigned into the copy and then nothing happens?

1 Like

Thanks both of you. This makes sense.