why doesn't `@.` always broadcast?

@. doesn’t introduce broadcasting to everything that isn’t broadcasting. In this case, indexing syntax in the macro’s input expression does not count as a function call, even though it will later lower to a getindex call. The docstring for @. is pretty clear on what it’s supposed to do, and the @macroexpand result is very readable if you need to check:

julia> @macroexpand @. x->x[1] # doesn't touch indexing syntax
:(x->begin
          #= REPL[1]:1 =#
          x[1]
      end)

julia> @macroexpand @. x->getindex(x,1) # affects function call
:(x->begin
          #= REPL[3]:1 =#
          getindex.(x, 1)
      end)

julia> @macroexpand @. x->getindex.(x,1) # leaves broadcasted calls alone
:(x->begin
          #= REPL[4]:1 =#
          getindex.(x, 1)
      end)

help?> @.
  @. expr

  Convert every function call or operator in expr into a "dot call" (e.g. convert f(x) to f.(x)), and convert every
  assignment in expr to a "dot assignment" (e.g. convert += to .+=).

  If you want to avoid adding dots for selected function calls in expr, splice those function calls in with $. For
  example, @. sqrt(abs($sort(x))) is equivalent to sqrt.(abs.(sort(x))) (no dot for sort).
7 Likes