First we have to address that without the @view
macro, that code would cause another error:
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> @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:
@views function fun(x, y, t)
result = rand(3,3)
y[:, :, t] = result
x[:, :, t] = result
return nothing
end