# Marking types as scalar for broadcasting, Ref vs. Tuple?

**URL:** <https://discourse.julialang.org/t/marking-types-as-scalar-for-broadcasting-ref-vs-tuple/29105>\
**Category:** General Usage\
**Created:** [September 24, 2019, 12:23pm UTC](https://discourse.julialang.org/t/marking-types-as-scalar-for-broadcasting-ref-vs-tuple/29105 "2019-09-24T12:23:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [September 24, 2019, 12:23pm UTC](https://discourse.julialang.org/t/marking-types-as-scalar-for-broadcasting-ref-vs-tuple/29105/1 "2019-09-24T12:23:31Z")

</div>

The typical method of marking a type as scalar for broadcasting seems to be

```
Base.Broadcast.broadcastable(x::MyType) = Ref(x)

```

I’ve experimented a bit with

```
Base.Broadcast.broadcastable(x::MyType) = (x,)

```

as an alternative, to avoid allocation of the `Ref` (or is that always elided?). Seems to work fine, but I wonder - is there any downside to using a `Tuple` here that I just haven’t run into yet?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [September 24, 2019, 12:35pm UTC](https://discourse.julialang.org/t/marking-types-as-scalar-for-broadcasting-ref-vs-tuple/29105/2 "2019-09-24T12:35:23Z")

</div>

Quoting Matt from [https://discourse.julialang.org/t/recent-broadcast-changes-iterate-by-default-scalar-struct-and/11178/12:](https://discourse.julialang.org/t/recent-broadcast-changes-iterate-by-default-scalar-struct-and/11178/12:)

> You can also use a tuple literal like `@. f(x, (y,))` to treat `y` kind-of like a scalar. For most purposes this should be just as fast and scalar-like as a `Ref` , but it’s not 100% scalar-like: if `x` is scalar-like or a zero-dimensional array, `f.(x, Ref(y))` will return an unwrapped scalar, whereas `f.(x, (y,))` will return a 1-tuple. Some array types might also not support broadcasting with tuples as nicely as they do scalars.

So you can compare e.g.

```julia
julia> f(x, y) = x + y;

julia> f.(1, Ref(2))
3

julia> f.(1, (2,))
(3,)

```

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [September 24, 2019, 12:50pm UTC](https://discourse.julialang.org/t/marking-types-as-scalar-for-broadcasting-ref-vs-tuple/29105/3 "2019-09-24T12:50:49Z")

</div>

Thanks, @fredrikekre!
