# Broadcasting \`$\` as a binary operator

**URL:** https://discourse.julialang.org/t/broadcasting-as-a-binary-operator/121229
**Category:** Internals & Design
**Tags:** question, broadcast
**Created:** [October 12, 2024, 4:01pm UTC](https://discourse.julialang.org/t/broadcasting-as-a-binary-operator/121229 "2024-10-12T16:01:31Z")
**Posts on this page:** 8
**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: [October 12, 2024, 4:01pm UTC](https://discourse.julialang.org/t/broadcasting-as-a-binary-operator/121229/1 "2024-10-12T16:01:31Z")

</div>

It’s possible to define `$` as a binary operator, but it turns out you can’t use it with broadcasting:

```julia-repl
julia> ($)(x, y) = x + y
$ (generic function with 1 method)

julia> 1 $ 2
3

julia> [1, 2] .$ [3, 4]
ERROR: ParseError:
# Error @ REPL[3]:1:7
[1, 2] .$ [3, 4]
# ╙ ── whitespace is not allowed here

```

This seems to be intentional, since the old `julia-parser.scm` file has this code in it, where `$` is explicitly excluded from the the `add-dots` call:

```julia
(define prec-plus (append! '($) (add-dots '(+ - # ...

```

I’m guessing it has to do with a parsing ambiguity related to expression interpolation, or something like that, but I can’t point to exactly what the issue is. Does anyone know why binary `$` is excluded from broadcasting?

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 12, 2024, 4:52pm UTC](https://discourse.julialang.org/t/broadcasting-as-a-binary-operator/121229/2 "2024-10-12T16:52:56Z")

</div>

Just as a side note, broadcasting works without the dot fusion:

```julia
broadcast($, [1, 2], [3, 4])

```

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [October 12, 2024, 5:03pm UTC](https://discourse.julialang.org/t/broadcasting-as-a-binary-operator/121229/3 "2024-10-12T17:03:35Z")

</div>

what

---

<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: [October 12, 2024, 5:07pm UTC](https://discourse.julialang.org/t/broadcasting-as-a-binary-operator/121229/4 "2024-10-12T17:07:55Z")

</div>

> [@rafael.guerra](#):
>
> ```julia
> broadcast($, [1, 2], [3, 4])
> 
> ```

Yeah, it also works with `@.`:

```julia-repl
julia> @. [1, 2] $ [3, 4]
2-element Vector{Int64}:
 4
 6

```

---

<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: [October 12, 2024, 7:05pm UTC](https://discourse.julialang.org/t/broadcasting-as-a-binary-operator/121229/5 "2024-10-12T19:05:36Z")

</div>

> [@CameronBieganek](#):
>
> I’m guessing it has to do with a parsing ambiguity related to expression interpolation, or something like that, but I can’t point to exactly what the issue is. Does anyone know why binary `$` is excluded from broadcasting?

From [parse more dot operators, parse .= with assignment precedence by stevengj · Pull Request #17393 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/17393) :

> The exceptions are operators with very special syntactic meanings, like `$` and `||`. (Some of these could be allowed, but I wanted to be conservative to start with).

The case of `$` was never re-visited, apparently.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [October 12, 2024, 7:15pm UTC](https://discourse.julialang.org/t/broadcasting-as-a-binary-operator/121229/6 "2024-10-12T19:15:47Z")

</div>

> [@CameronBieganek](#):
>
> ```julia
> julia> 1 $ 2
> 3
> 
> ```

this by itself is so cursed already, feels like we should have disallowed that (given how `$` is used for “interpolation” in many context

---

<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: [October 12, 2024, 7:27pm UTC](https://discourse.julialang.org/t/broadcasting-as-a-binary-operator/121229/7 "2024-10-12T19:27:32Z")

</div>

> [@jling](#):
>
> > [@CameronBieganek](#):
> >
> > ```julia
> > julia> 1 $ 2
> > 3
> > 
> > ```
> 
> this by itself is so cursed already

Yeah, the meaning of `$` is heavily overloaded in that case, but it could be nice for some things, like as a function application operator, which is how `$` is used in Haskell and in this Julia package:

> **[GitHub - archermarx/PartialFunctions.jl: A small package to simplify partial function...](https://github.com/archermarx/PartialFunctions.jl)**
>
> A small package to simplify partial function application

I had a use case in mind similar to function application.

---

<div class="post-metadata">

### Author: ![savq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/savq/32/22063_2.png) [@savq](https://discourse.julialang.org/u/savq)
#### Post date: [October 13, 2024, 12:03am UTC](https://discourse.julialang.org/t/broadcasting-as-a-binary-operator/121229/8 "2024-10-13T00:03:44Z")

</div>

I assume that `$` works this way to mirror `:`, which is also a syntactic operator in prefix form, but a function operator in infix form. Moreover, `:` cannot be broadcasted either.

(I don’t like it either 😠 )

Edit: To add to this, the exact ambiguities are the following:

```julia-repl
julia> p = :prop; :(obj.$p) # Not broadcasting, but interpolation
:(obj.prop)

julia> julia> :(a.:b) == :(a.b) # not broadcasting either
true

```
