# A more intuitive way to treat an object as a scalar in broadcasting?

**URL:** <https://discourse.julialang.org/t/a-more-intuitive-way-to-treat-an-object-as-a-scalar-in-broadcasting/35598>\
**Category:** Internals & Design\
**Tags:** broadcast\
**Created:** [March 5, 2020, 6:14pm UTC](https://discourse.julialang.org/t/a-more-intuitive-way-to-treat-an-object-as-a-scalar-in-broadcasting/35598 "2020-03-05T18:14:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [March 5, 2020, 6:14pm UTC](https://discourse.julialang.org/t/a-more-intuitive-way-to-treat-an-object-as-a-scalar-in-broadcasting/35598/1 "2020-03-05T18:14:03Z")

</div>

I think it would make sense to have a more intuitive way to treat an object as a scalar for broadcasting. The canonical example, which new users ask about all the time, is when using `in`:

```julia
julia> [2, 3, 4] .∈ Ref([1, 3, 5])
3-element BitArray{1}:
 0
 1
 0

```

I don’t think the use of `Ref` to denote that an object should be a scalar is very intuitive, especially for users new to the language. Furthermore, this usage is not mentioned anywhere in the official docs that I can find, not even in the docstring for `in`. One has to stumble upon it on Discourse or Stack Overflow.

However, I don’t have a concrete suggestion for what the preferred syntax should be…

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 5, 2020, 6:17pm UTC](https://discourse.julialang.org/t/a-more-intuitive-way-to-treat-an-object-as-a-scalar-in-broadcasting/35598/2 "2020-03-05T18:17:08Z")

</div>

If performance isn’t an issue, I go for `[2,3,4] .∈ [[1,3,5]]`. If there’s performance on the table then I use a 1-tuple instead (it requires one extra character ,). I find `Ref` cumbersome indeed — and the reason why it’s been promoted as an alternative is that there was a proposal to use `&x` for `Ref(x)` which could then be shared between `ccall` and broadcast, but it went nowhere.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [March 5, 2020, 6:27pm UTC](https://discourse.julialang.org/t/a-more-intuitive-way-to-treat-an-object-as-a-scalar-in-broadcasting/35598/3 "2020-03-05T18:27:51Z")

</div>

Yeah, I suppose `[2,3,4] .∈ ([1,3,5],)` is not too bad, now that I think about it. I should start using that instead of `Ref`. 🙂 I guess I’m biased because I come from R where I can do `c(2, 3, 4) %in% c(1, 3, 5)` and it “just works” the way I expect.

**Edit:**

Adding an example like

```julia
julia> [2, 3, 4] .∈ ([1, 3, 5],)
3-element BitArray{1}:
 0
 1
 0

```

to the docstring for `in` would probably be a good idea.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 5, 2020, 6:30pm UTC](https://discourse.julialang.org/t/a-more-intuitive-way-to-treat-an-object-as-a-scalar-in-broadcasting/35598/4 "2020-03-05T18:30:32Z")

</div>

This was definitely a pain point for me initially, same use-case even. I previously did use the array or the tuple approach, but then I saw the suggestion to use `Ref`. I actually don’t find it too cumbersome to use although I agree that it isn’t something you are likely to stumble upon on your own.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [March 5, 2020, 7:18pm UTC](https://discourse.julialang.org/t/a-more-intuitive-way-to-treat-an-object-as-a-scalar-in-broadcasting/35598/5 "2020-03-05T19:18:41Z")

</div>

I think it was once proposed to introduce a dedicated `Scalar` type, which I believe would be much more intuitive for newcomers to learn about. One problem with `Ref` is also that it’s mutable and will therefore always allocate, whereas `Scalar` could just be an immutable struct with a very minimal implementation.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 5, 2020, 7:30pm UTC](https://discourse.julialang.org/t/a-more-intuitive-way-to-treat-an-object-as-a-scalar-in-broadcasting/35598/6 "2020-03-05T19:30:55Z")

</div>

I could get behind that, but calling it `Scalar` just feels wrong to me. It’s not a scalar; it’s a 1-element container! While it makes sense that you want broadcast to _treat_ the thing it contains as a scalar, that’s not at all what the object itself behaves like.

So, yeah, the word `Scalar` might make sense to a naive user in the context of broadcast, but it’s wholly confusing everywhere else.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [March 5, 2020, 8:22pm UTC](https://discourse.julialang.org/t/a-more-intuitive-way-to-treat-an-object-as-a-scalar-in-broadcasting/35598/7 "2020-03-05T20:22:18Z")

</div>

> [@CameronBieganek](#):
>
> Adding an example like
> 
> ```julia
> julia> [2, 3, 4] .∈ ([1, 3, 5],)
> 3-element BitArray{1}:
> 0
> 1
> 0
> 
> ```
> 
> to the docstring for `in` would probably be a good idea.

Good idea, would you make a pull request to add this? You can edit the file directly on GitHub.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 5, 2020, 10:00pm UTC](https://discourse.julialang.org/t/a-more-intuitive-way-to-treat-an-object-as-a-scalar-in-broadcasting/35598/8 "2020-03-05T22:00:43Z")

</div>

There was a suggestion to use `&x` as sugar for this ([https://github.com/JuliaLang/julia/pull/27608](https://github.com/JuliaLang/julia/pull/27608)).

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [March 5, 2020, 10:13pm UTC](https://discourse.julialang.org/t/a-more-intuitive-way-to-treat-an-object-as-a-scalar-in-broadcasting/35598/9 "2020-03-05T22:13:07Z")

</div>

I think the single element tuple is clear in its purpose. I wouldn’t want another way to do it unless it was somehow more efficient.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [March 5, 2020, 10:22pm UTC](https://discourse.julialang.org/t/a-more-intuitive-way-to-treat-an-object-as-a-scalar-in-broadcasting/35598/10 "2020-03-05T22:22:24Z")

</div>

> [@nalimilan](#):
>
> would you make a pull request to add this?

Sure thing:

> <https://github.com/JuliaLang/julia/pull/35023>
>
> This PR adds examples to the \`in\` and \`∉\` docstrings that show how to use those …infix operators in broadcasting expressions where the collection on the right side is intended to be treated as scalar. I've included one example with the right side enclosed in a tuple and one without the tuple to demonstrate the difference between the two, since the latter is also valid syntax that could easily be confused with the right-side-as-a-scalar behavior.
