# Help implementing \`copyto!\` for broadcasting

**URL:** <https://discourse.julialang.org/t/help-implementing-copyto-for-broadcasting/51204>\
**Category:** New to Julia\
**Created:** [December 3, 2020, 3:42pm UTC](https://discourse.julialang.org/t/help-implementing-copyto-for-broadcasting/51204 "2020-12-03T15:42:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 3, 2020, 3:42pm UTC](https://discourse.julialang.org/t/help-implementing-copyto-for-broadcasting/51204/1 "2020-12-03T15:42:44Z")

</div>

I have a type `DataRow`. It’s goal is to act like a row of a `DataFrame`, but standing on its own.

1. It’s one dimensional
2. It’s indexable
3. It iterates through values
4. It stores values in a Vector{Any} internally

I’m trying to extend in-place broadcasting, via [this link](https://docs.julialang.org/en/v1/manual/interfaces/#extending-in-place-broadcast-2).

If `r` is a `DataRow` with two elements, I would like

```julia
r .= [3, 4]

```

to update the values of `r`. I’ve implemented

```julia

Base.BroadcastStyle(::Type{<:DataRow}) = Broadcast.Style{DataRow}()

function copyto!(dest::DataRow, bc::Broadcasted{Broadcast.DefaultArrayStyle{1}})
	println("success")
	nothing
end

```

This feels like the method to implement, but I haven’t yet been able to hit this method

```julia
julia> r .= [1, 2]
ERROR: MethodError: no method matching copyto!(::DataRow, ::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Tuple{Base.OneTo{Int64}},typeof(identity),Tuple{Array{Int64,1}}})

```

Overall I’m pretty lost about how to implement broadcasting. Any help is appreciated, particularly if someone could point me to a good implementation in some package that I can copy

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [December 3, 2020, 3:58pm UTC](https://discourse.julialang.org/t/help-implementing-copyto-for-broadcasting/51204/2 "2020-12-03T15:58:28Z")

</div>

You know we have implemented it in DataFrames.jl 😃. Here is an implementation: [https://github.com/JuliaData/DataFrames.jl/blob/master/src/other/broadcasting.jl#L216](https://github.com/JuliaData/DataFrames.jl/blob/master/src/other/broadcasting.jl#L216) (it is more complex than you need but hopefully will be useful as a template). If you have specific questions please let me know.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [December 3, 2020, 4:04pm UTC](https://discourse.julialang.org/t/help-implementing-copyto-for-broadcasting/51204/3 "2020-12-03T16:04:56Z")

</div>

A quick fix that might help is that you should use `Base.copyto!` unless you explicitly import `copyto!`.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 3, 2020, 4:59pm UTC](https://discourse.julialang.org/t/help-implementing-copyto-for-broadcasting/51204/4 "2020-12-03T16:59:12Z")

</div>

I think this is correct?

```julia
Base.BroadcastStyle(::Type{<:DataRow}) = Broadcast.Style{DataRow}()

function Base.copyto!(r::DataRow, bc::Broadcasted)
	bcf = Broadcast.flatten(bc)
	bcf2 = Base.Broadcast.preprocess(r, bcf)

	for i in 1:length(r)
		r[i] = bcf2[i]
	end
	r
end

```

EDIT : I have no idea what these `preprocess` steps are. Can someone help me understand tem?

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [December 3, 2020, 5:04pm UTC](https://discourse.julialang.org/t/help-implementing-copyto-for-broadcasting/51204/5 "2020-12-03T17:04:32Z")

</div>

I am not sure you need to `flatten` (you are not using any internal structure of `bc` yet in your code), `preprocess` is probably needed (to do unaliasing). Now for `1:length(r)` is probably safe, but a normal thing would be to use `axes(bcf2, 1)` (but I think it should be the same in this case as you are not accepting non standard indexing in `bcf2` anyway - right?).
