# Content Copy (.= Operator) for Composite Types (structs)?

**URL:** <https://discourse.julialang.org/t/content-copy-operator-for-composite-types-structs/10330>\
**Category:** General Usage\
**Created:** [April 14, 2018, 5:14am UTC](https://discourse.julialang.org/t/content-copy-operator-for-composite-types-structs/10330 "2018-04-14T05:14:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [April 14, 2018, 5:14am UTC](https://discourse.julialang.org/t/content-copy-operator-for-composite-types-structs/10330/1 "2018-04-14T05:14:43Z")

</div>

For arrays, we have a beautiful new `.=` operator that copies the contents of the right to the left.

For structs, this is not working:

```julia
julia> mutable struct Point; x; y; end#struct

julia> p= Point(2,3)
Point(2, 3)

julia> n= Point(5,10)
Point(5, 10)

julia> n .= p
ERROR: MethodError: no method matching broadcast!(::Base.#identity, ::Point, ::Point)
Closest candidates are:
  broadcast!(::Any, ::AbstractArray, ::Any, ::Any...) where N at broadcast.jl:206
  broadcast!(::Base.#identity, ::Array{T,N}, ::Array{S,N}) where {T, S, N} at broadcast.jl:18
  broadcast!(::Base.#identity, ::AbstractArray, ::Number) at broadcast.jl:22
  ...

```

would this be a good language addition? what is a good way to define this myself? the following seems fairly ugly and becomes unwieldy for large structs.

```julia
copycontents(l,r) = ( l.x=r.x; l.y=r.y; l )

```

/iaw

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 14, 2018, 6:23am UTC](https://discourse.julialang.org/t/content-copy-operator-for-composite-types-structs/10330/2 "2018-04-14T06:23:00Z")

</div>

The [dot syntax](https://docs.julialang.org/en/latest/manual/functions/#man-vectorized-1) invokes the [broadcasting interface](https://docs.julialang.org/en/latest/manual/interfaces/#man-interfaces-broadcasting-1). A key design decision here is whether (`mutable`) `struct`s should be considered containers over which one can broadcast.

This can make sense where the operation is sufficiently generic so that it works for all fields. I am skeptical about enabling this in general, since I usually use structures for heterogeneous collections.

The `.=` is a special case, but I would rather implement a method for `copy!`. This would also sidestep the whole promotion machinery for broadcasting; provided one does not want `somestruct .= somearray` and things like that.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [April 14, 2018, 1:02pm UTC](https://discourse.julialang.org/t/content-copy-operator-for-composite-types-structs/10330/3 "2018-04-14T13:02:21Z")

</div>

You can just do

```julia
Base.broadcast!(::typeof(identity), dest::Point, src::Point) = (dest.x=src.x; dest.y=src.y; dest)
n .= p;

```

If you really, really want you could try something like

```julia
@generated function Base.broadcast!(::typeof(identity), dest::T, src::T) where T
...
end

```

But I think that this default would be too dangerous (if your mutable has mutable fields, how deep do you copy? What about pointer fields? Handles to system resources?).

---

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [April 14, 2018, 4:04pm UTC](https://discourse.julialang.org/t/content-copy-operator-for-composite-types-structs/10330/4 "2018-04-14T16:04:17Z")

</div>

thank you, tamas and foobar. I am beginning to appreciate the devils in the detail here. yes, this should not be a default. maybe an easier way to allow users to state that this is what they want? not sure.
