Hello. I’m having some trouble understanding Julia’s broadcast behaviour on a specific scenario.
The following code works as expected:
julia> a = [1, 2, 3];
julia> refs = [Ref(a, i) for i in eachindex(a)];
julia> refs[1][] = 0;
julia> refs[2][] = 0;
julia> a
3-element Vector{Int64}:
0
0
3
But brodcasting the operation via @.
gives an error.
julia> @. refs[1:2][] = 0
ERROR: BoundsError: attempt to access 2-element Vector{Base.RefArray{Int64, Vector{Int64}, Nothing}} at index []
Stacktrace:
[1] throw_boundserror(A::Vector{Base.RefArray{Int64, Vector{Int64}, Nothing}}, I::Tuple{})
@ Base ./abstractarray.jl:703
[2] checkbounds
@ ./abstractarray.jl:668 [inlined]
[3] _getindex
@ ./abstractarray.jl:1273 [inlined]
[4] getindex
@ ./abstractarray.jl:1241 [inlined]
[5] maybeview
@ ./views.jl:149 [inlined]
[6] dotview(args::Vector{Base.RefArray{Int64, Vector{Int64}, Nothing}})
@ Base.Broadcast ./broadcast.jl:1201
[7] top-level scope
@ REPL[7]:1
I’m aware that one can achieve the desired effect via setindex!.
, but I’m still interested in better understanding where the error comes from (I guess it has to do with the broadcast dereferencing things that I don’t want it to). For example, is there a way to use $
to make the @.
line work?
Thank you!