# Add @scalar macro to Base?

**URL:** https://discourse.julialang.org/t/add-scalar-macro-to-base/50255
**Category:** Internals & Design
**Tags:** broadcast
**Created:** [November 16, 2020, 10:28pm UTC](https://discourse.julialang.org/t/add-scalar-macro-to-base/50255 "2020-11-16T22:28:53Z")
**Posts on this page:** 9
**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: [November 16, 2020, 10:28pm UTC](https://discourse.julialang.org/t/add-scalar-macro-to-base/50255/1 "2020-11-16T22:28:53Z")

</div>

Would it be useful if Base exported an `@scalar` macro? The usage of `@scalar` would be to declare that a struct behaves as a scalar for broadcasting. So this

```julia
@scalar struct A end

```

would be re-written to this:

```julia
struct A end
Base.broadcastable(x::A) = Ref(x)

```

This would probably encourage package authors to declare structs as scalars when they are meant to be scalars. The advantage of that would be that in many places we could change our code from this

```julia
foo.(Ref(A), [1, 2])

```

to this:

```julia
foo.(A, [1, 2])

```

---

<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: [November 17, 2020, 8:52am UTC](https://discourse.julialang.org/t/add-scalar-macro-to-base/50255/2 "2020-11-17T08:52:40Z")

</div>

The problem with macros before `struct` is that they don’t combine, the next one (`Base.@kwdef`, or one from a package) may get an AST that it cannot parse. Because of this, I think it would be better to have a macro like

```nohighlight
@scalar A

```

but perhaps with more expressive name.

---

<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: [November 17, 2020, 5:10pm UTC](https://discourse.julialang.org/t/add-scalar-macro-to-base/50255/3 "2020-11-17T17:10:49Z")

</div>

> [@Tamas\_Papp](#):
>
> but perhaps with more expressive name

I guess it could be

```julia
@broadcastable_scalar A

```

but at that point it’s not much better than

```julia
Base.broadcastable(x::A) = Ref(x)

```

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [November 17, 2020, 5:14pm UTC](https://discourse.julialang.org/t/add-scalar-macro-to-base/50255/4 "2020-11-17T17:14:59Z")

</div>

As an aside, FWIW, Mason Protter posted an example somewhere of constprop working with

```julia
Base.broadcastable(x::A) = (x,)

```

but not with

```julia
Base.broadcastable(x::A) = Ref(x)

```

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [November 17, 2020, 5:20pm UTC](https://discourse.julialang.org/t/add-scalar-macro-to-base/50255/5 "2020-11-17T17:20:49Z")

</div>

There’s a problem though with using `(x,)` generically:

```julia
julia> 1 .+ (1,)
(2,)

julia> 1 .+ Ref(1)
2

```

This is because as far as broadcast is concerned, `(1,)` is a 1D container whereas `Ref(1)` is a 0D container. This is rarely if ever a problem in real-world broadcasting code, but can cause some trickyness if you make it the default.

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [November 17, 2020, 5:25pm UTC](https://discourse.julialang.org/t/add-scalar-macro-to-base/50255/6 "2020-11-17T17:25:01Z")

</div>

Seems we should add a 0d `struct` container for this.

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [November 17, 2020, 5:26pm UTC](https://discourse.julialang.org/t/add-scalar-macro-to-base/50255/7 "2020-11-17T17:26:21Z")

</div>

Yeah, I had a PR where I had hoped we could just make `Some` work for this since it was already in `Base`, but it was decided to not be a great idea, but we still dont have a great alternative [https://github.com/JuliaLang/julia/pull/35778](https://github.com/JuliaLang/julia/pull/35778)

---

<div class="post-metadata">

### Author: ![datnamer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datnamer/32/3471_2.png) [@datnamer](https://discourse.julialang.org/u/datnamer)
#### Post date: [November 17, 2020, 5:34pm UTC](https://discourse.julialang.org/t/add-scalar-macro-to-base/50255/8 "2020-11-17T17:34:45Z")

</div>

isn’t this just another special case workaround for lack of traits or multiple inheritance?

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [November 17, 2020, 5:54pm UTC](https://discourse.julialang.org/t/add-scalar-macro-to-base/50255/9 "2020-11-17T17:54:00Z")

</div>

No, not really. Broadcast is already a trait based system. We’re using traits here.
