# Broadcasting over structs in 1.0: what am I supposed to do?

**URL:** <https://discourse.julialang.org/t/broadcasting-over-structs-in-1-0-what-am-i-supposed-to-do/13408>\
**Category:** General Usage\
**Created:** [August 13, 2018, 9:13pm UTC](https://discourse.julialang.org/t/broadcasting-over-structs-in-1-0-what-am-i-supposed-to-do/13408 "2018-08-13T21:13:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [August 13, 2018, 9:13pm UTC](https://discourse.julialang.org/t/broadcasting-over-structs-in-1-0-what-am-i-supposed-to-do/13408/1 "2018-08-13T21:13:19Z")

</div>

Hi all-

I commonly broadcast over structs with the dot syntax. While updating my code, I was dismayed to see this is longer a simple process. Here is a minimum working example just for reference:

```julia
mutable struct M
    x::Int 
end 
m = M(1)
f(m,x) = x/m.x
y = [1,2,3]
f.(m,y)

```

This code results in the following warning:

```julia
┌ Warning: broadcast will default to iterating over its arguments in the future. Wrap arguments of
│ type `x::M` with `Ref(x)` to ensure they broadcast as "scalar" elements.
│ caller = ip:0x0
└ @ Core :-1

```

I was further dismayed because the warning message is confusing. It seems to indicate (1) I will (maybe?) have to use Ref(X) all over my code and (2) something will (might?) change in the future. After digging around, I encountered a [long discussion](https://discourse.julialang.org/t/recent-broadcast-changes-iterate-by-default-scalar-struct-and/11178) that left me even more confused about the situation and what to do. Do I wrap my arguments like so: `f.(Ref(m),y)`? Do I simply declare `Broadcast.broadcastable(x::M) = Ref(x)` for each struct? Both currently work, but will it change? (And for what it’s worth if an extra step is necessary for broadcasting structs would strongly prefer to type `Broadcast.broadcastable(x::M) = Ref(x)` once rather than `Ref(x)` all over my code). I’m hoping someone one can clarify the situation so I can know how to proceed

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [August 13, 2018, 9:26pm UTC](https://discourse.julialang.org/t/broadcasting-over-structs-in-1-0-what-am-i-supposed-to-do/13408/2 "2018-08-13T21:26:01Z")

</div>

```julia
julia> Broadcast.broadcastable(m::M) = Ref(m)

julia> f.(m,y)
3-element Array{Float64,1}:
 1.0
 2.0
 3.0

```

See help on `broadcastable` and [https://docs.julialang.org/en/latest/manual/interfaces/#man-interfaces-broadcasting-1](https://docs.julialang.org/en/latest/manual/interfaces/#man-interfaces-broadcasting-1) for further information.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [August 13, 2018, 9:33pm UTC](https://discourse.julialang.org/t/broadcasting-over-structs-in-1-0-what-am-i-supposed-to-do/13408/3 "2018-08-13T21:33:13Z")

</div>

Thank you for clarifying. I was looking for this information in the [Broadcasting and Vectorization section.](https://docs.julialang.org/en/stable/base/arrays/#Base.Broadcast.broadcastable)

---

<div class="post-metadata">

**Author:** ![1115](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1115/32/4465_2.png) [@1115](https://discourse.julialang.org/u/1115)\
**Post date:** [September 26, 2018, 6:52am UTC](https://discourse.julialang.org/t/broadcasting-over-structs-in-1-0-what-am-i-supposed-to-do/13408/4 "2018-09-26T06:52:59Z")

</div>

Thanks, but shouldn’t “being broadcasted by Ref” be a default behavior of an instance?

The fact is, only when I use broadcasting, I will remember to define this trivil interface to prevent error.  
On the other hand, adding this natural behavior to every struct is too expensive for developer.

Is this discussion serious?!  
[https://github.com/JuliaLang/julia/pull/26435](https://github.com/JuliaLang/julia/pull/26435)

I will comment back!
