# How does broadcasting work for a vector of \`RefArray\`s?

**URL:** <https://discourse.julialang.org/t/how-does-broadcasting-work-for-a-vector-of-refarray-s/91326>\
**Category:** General Usage\
**Created:** [December 6, 2022, 10:42pm UTC](https://discourse.julialang.org/t/how-does-broadcasting-work-for-a-vector-of-refarray-s/91326 "2022-12-06T22:42:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArthurW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arthurw/32/19561_2.png) [@ArthurW](https://discourse.julialang.org/u/ArthurW)\
**Post date:** [December 6, 2022, 10:42pm UTC](https://discourse.julialang.org/t/how-does-broadcasting-work-for-a-vector-of-refarray-s/91326/1 "2022-12-06T22:42:00Z")

</div>

Hello. I’m having some trouble understanding Julia’s broadcast behaviour on a specific scenario.

The following code works as expected:

```julia
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
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!

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [December 6, 2022, 11:10pm UTC](https://discourse.julialang.org/t/how-does-broadcasting-work-for-a-vector-of-refarray-s/91326/2 "2022-12-06T23:10:03Z")

</div>

The macro `@.` only broadcasts function calls or operators; it does not work with indexing or dereferencing. So you are having the same error as if you just typed the left hand side of the operation:

```julia
refs[1:2][]

```

---

<div class="post-metadata">

**Author:** ![ArthurW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arthurw/32/19561_2.png) [@ArthurW](https://discourse.julialang.org/u/ArthurW)\
**Post date:** [December 7, 2022, 10:44am UTC](https://discourse.julialang.org/t/how-does-broadcasting-work-for-a-vector-of-refarray-s/91326/3 "2022-12-07T10:44:08Z")

</div>

I see. IDK why, but I was expecting `@.` to do a something more ellaborate. My bad.  
That was a quick one. Thank you!

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 7, 2022, 11:41am UTC](https://discourse.julialang.org/t/how-does-broadcasting-work-for-a-vector-of-refarray-s/91326/4 "2022-12-07T11:41:52Z")

</div>

if you use the setindex!() function as defined in refpointer.jl

```julia
a = [1, 2, 3];
refs = [Ref(a, i) for i in eachindex(a)]
struct RefArray{T,A<:AbstractArray{T},R} <: Ref{T}
    x::A
    i::Int
    roots::R # should be either ::Nothing or ::Any
    RefArray{T,A,R}(x,i,roots=nothing) where {T,A<:AbstractArray{T},R} = new(x,i,roots)
end

Base.setindex!(b::RefArray, x) = (b.x[b.i] = x; b)

@. setindex!(refs[1:2],-1)
julia> a
3-element Vector{Int64}:
 -1
 -1
  3

```
