# Broadcast for vectors of vector like objects

**URL:** <https://discourse.julialang.org/t/broadcast-for-vectors-of-vector-like-objects/7226>\
**Category:** General Usage\
**Tags:** broadcast\
**Created:** [November 21, 2017, 6:46pm UTC](https://discourse.julialang.org/t/broadcast-for-vectors-of-vector-like-objects/7226 "2017-11-21T18:46:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gasagna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasagna/32/1275_2.png) [@gasagna](https://discourse.julialang.org/u/gasagna)\
**Post date:** [November 21, 2017, 6:46pm UTC](https://discourse.julialang.org/t/broadcast-for-vectors-of-vector-like-objects/7226/1 "2017-11-21T18:46:27Z")

</div>

Say I have an object `Foo` for which broadcast is defined

```julia
struct Foo{T, V<:AbstractVector{T}} 
    data::V
end

Foo(data::AbstractVector) = Foo{eltype(data), typeof(data)}(data)

@generated function Base.Broadcast.broadcast!(f, u::Foo, args::Vararg{Any, n}) where {n}
    quote 
        $(Expr(:meta, :inline))
        broadcast!(f, unsafe_get(u), map(unsafe_get, args)...)
        u
    end
end

Base.unsafe_get(u::Foo) = u.data

```

Say I have two vectors of `Foo`s, as in

```julia
w = [Foo([1, 2]), Foo([3, 4])]
v = [Foo([2, 3]), Foo([5, 6])]

```

I would like to have code such that this broadcast operation

```julia
w .+= v

```

works, and gives

```julia
w = [Foo([3, 5]), Foo([8, 10])]

```

with no temporary allocations, because the dot notation is transferred efficiently down a level to `Foo` objects. A quick look on github shows that there is an open issue about this, that means it is a known thing.

Has anyone found a good way to express this? Rather than a `Vector` of `Foo`s, in actual code I have a custom container type wrapping a `Vector` so I can optionally define broadcast for this wrapper as well if that is needed.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 21, 2017, 8:47pm UTC](https://discourse.julialang.org/t/broadcast-for-vectors-of-vector-like-objects/7226/2 "2017-11-21T20:47:09Z")

</div>

> [@gasagna](#):
>
> Has anyone found a good way to express this?

I use a `ArrayPartition`s for this kind of thing all the time:

> **[GitHub - SciML/RecursiveArrayTools.jl: Tools for easily handling objects like...](https://github.com/SciML/RecursiveArrayTools.jl#arraypartition)**
>
> Tools for easily handling objects like arrays of arrays and deeper nestings in scientific machine learning (SciML) and other applications - GitHub - SciML/RecursiveArrayTools.jl: Tools for easily h...

`VectorOfArray`s will also do it, but it will use the indexing because the broadcast PR isn’t done yet. But broadcasting that kind of thing would use the same overloads as an `ArrayPartition`, so that code is somewhat a tutorial right now.
