# Broadcasting structs as scalars

**URL:** <https://discourse.julialang.org/t/broadcasting-structs-as-scalars/14310>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 30, 2018, 12:07pm UTC](https://discourse.julialang.org/t/broadcasting-structs-as-scalars/14310 "2018-08-30T12:07:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [August 30, 2018, 12:07pm UTC](https://discourse.julialang.org/t/broadcasting-structs-as-scalars/14310/1 "2018-08-30T12:07:47Z")

</div>

The following code used to work in Julia v0.6.4, but it is not working in Julia v1.0.

```julia
struct Q
       x::Int
end
f(q::Q, x::Int) = q.x + x
f.(Q(2), [1,2,3])
# ERROR: MethodError: no method matching length(::Q)

```

What should I do to broadcast on `Q` as a scalar?

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [August 30, 2018, 12:11pm UTC](https://discourse.julialang.org/t/broadcasting-structs-as-scalars/14310/2 "2018-08-30T12:11:05Z")

</div>

```julia
Base.Broadcast.broadcastable(q::Q) = Ref(q) 

```

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [August 30, 2018, 12:12pm UTC](https://discourse.julialang.org/t/broadcasting-structs-as-scalars/14310/3 "2018-08-30T12:12:09Z")

</div>

> [@Evizero](#):
>
> ```julia
> Base.Broadcast.broadcastable(q::Q) = Ref(q) 
> 
> ```

What’s the reasoning for not having something like this as the default for new types?

Also, wrapping as a `Ref` does not incur any performance costs?

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [August 30, 2018, 12:17pm UTC](https://discourse.julialang.org/t/broadcasting-structs-as-scalars/14310/4 "2018-08-30T12:17:00Z")

</div>

> [@e3c6](#):
>
> What’s the reasoning for not having something like this as the default for new types?

> [@recent broadcast changes (iterate by default), scalar struct, and \`@.\`](https://discourse.julialang.org/t/recent-broadcast-changes-iterate-by-default-scalar-struct-and/11178/15):
>
> There’s also a number of asymmetries here: In the future, we’ll iterate over the object or throw an error. You can always treat objects as scalars, but you cannot always treat them as iterables. By defaulting to errors for non-iterables, it’s a great big signal for library creators that they can opt-in to acting like a scalar if it is appropriate. End-users can opt-in to scalar-like behaviors by wrapping in another broadcastable container (like Ref or 1-tuples or 0-dimensional arrays). Were…

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 30, 2018, 12:19pm UTC](https://discourse.julialang.org/t/broadcasting-structs-as-scalars/14310/5 "2018-08-30T12:19:29Z")

</div>

Also:

> [@PSA: use Julia 0.7 if you are upgrading](https://discourse.julialang.org/t/psa-use-julia-0-7-if-you-are-upgrading/13321):
>
> A lot of people are posting issues where their old code doesn’t work anymore on 1.0 because some function has been renamed or removed. The 0.7 release exists exactly for this reason: your old code will work on 0.7 and give you a specific warning about how to fix your code. If you have any old code, you should use 0.7 to get these warnings before switching to 1.0; even if you just have work habits that may need changing, you should probably use 0.7 for a while. If someone posts an issue like thi…

Although it wouldn’t have helped in this special case, the advice is generally sound if you’re porting old code (older than 0.7) to 0.7/1.0.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [August 30, 2018, 12:34pm UTC](https://discourse.julialang.org/t/broadcasting-structs-as-scalars/14310/6 "2018-08-30T12:34:16Z")

</div>

It also does not affect performance, [Deprecate defaulting to scalar in broadcast (take 2) by mbauman · Pull Request #26435 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/26435#issuecomment-372519152).
