# Broadcasting behavior of @. with functions?

**URL:** <https://discourse.julialang.org/t/broadcasting-behavior-of-with-functions/112833>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 11, 2024, 3:32pm UTC](https://discourse.julialang.org/t/broadcasting-behavior-of-with-functions/112833 "2024-04-11T15:32:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hatmatrix](https://avatars.discourse-cdn.com/v4/letter/h/5f8ce5/32.png) [@hatmatrix](https://discourse.julialang.org/u/hatmatrix)\
**Post date:** [April 11, 2024, 3:32pm UTC](https://discourse.julialang.org/t/broadcasting-behavior-of-with-functions/112833/1 "2024-04-11T15:32:15Z")

</div>

I have a question regarding the use of the `@.` macro as it does not work when used to broadcast the result of an anonymous function.

Here is an example below which works.

```julia
x = 1.
z = [1., 1.]
@. (z - x)

```

Now I replace `z` with an anonymous function:

```julia
x = 1.
z = (n) -> repeat([1.], n)
@. (z(2) - x) 

```

and I get the error:

> MethodError: no method matching -(::Vector{Float64}, ::Float64)  
> For element-wise subtraction, use broadcasting with dot syntax: array .- scalar

Using `.-` indeed works.

```julia
x = 1.
z = (n) -> repeat([1.], n)
(z(2) .- x) 

```

I don’t understand though as I expect `@. (z(x) - x)` expands to `z.(2) .- x`.

```julia
julia> using MacroTools
julia> MacroTools.@expand @. (z(2) - x) 
:((-).(z.(2), x))

```

The only difference is `z.(2)` which is equal to `z(2)` in this case.

```julia
julia> isequal(z(2), z.(2))
true

```

So… why does the `@.` macro not work?

**Edit.** I guess it is the same reason why `z.(2) .- x` does not work while `z(2) .- x` does, even though `z.(2)` and `z(2)` are the same, which I still don’t understand.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [April 11, 2024, 3:36pm UTC](https://discourse.julialang.org/t/broadcasting-behavior-of-with-functions/112833/2 "2024-04-11T15:36:24Z")

</div>

> [@hatmatrix](#):
>
> **Edit.** I guess it is the same reason why `z.(2) .- x` does not work while `z(2) .- x` does

this is correct. note that you can opt-out expressions from inside `@.` via `$` like so

```julia
julia> @. ($z(2) - x)
2-element Vector{Float64}:
 0.0
 0.0

```

> `z.(2)` and `z(2)` are the same, which I still don’t understand.

this is simply because integers are iterable, unfortunately

```julia
julia> collect(i for i in 2)
0-dimensional Array{Int64, 0}:
2

```

edit:  
eesh there is indeed some extra confusing thing going on with dot fusion

---

<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:** [April 11, 2024, 3:40pm UTC](https://discourse.julialang.org/t/broadcasting-behavior-of-with-functions/112833/3 "2024-04-11T15:40:31Z")

</div>

I think this is the inverse of [broadcast should not drop zero-dimensional results to scalars · Issue #28866 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/28866#issuecomment-496999603), which is interesting. In short `z(2)` and `z.(2)` are only the same at the _end_ of a broadcast expression because Julia removes the 0-d container at the end but not in the middle.

---

<div class="post-metadata">

**Author:** ![hatmatrix](https://avatars.discourse-cdn.com/v4/letter/h/5f8ce5/32.png) [@hatmatrix](https://discourse.julialang.org/u/hatmatrix)\
**Post date:** [April 11, 2024, 7:00pm UTC](https://discourse.julialang.org/t/broadcasting-behavior-of-with-functions/112833/5 "2024-04-11T19:00:59Z")

</div>

Ok I think I got it.

`z.(2) .- x` is equivalent to `[z(i) - x for i in 2]`.

`z(2) .- x` is equivalent to `[i - x for i in z(2)]`.

Thanks for the `$`. I was trying `Ref` on `2` like `z.(Ref(2))` (scalar still being iterable) and `Ref(z.(2))` (argument not a scalar) but I see now why they don’t work.

---

<div class="post-metadata">

**Author:** ![hatmatrix](https://avatars.discourse-cdn.com/v4/letter/h/5f8ce5/32.png) [@hatmatrix](https://discourse.julialang.org/u/hatmatrix)\
**Post date:** [April 11, 2024, 7:03pm UTC](https://discourse.julialang.org/t/broadcasting-behavior-of-with-functions/112833/6 "2024-04-11T19:03:33Z")

</div>

Thanks. [This link](https://julialang.org/blog/2017/01/moredots/) from that other thread was kind of helpful and more confusing at the same time.
