Space issue with the @. macro

Is this normal/documented ?

julia> x = 1
1

julia> b = x -3 * x^2
-2

julia> x = [1,2,3]
3-element Vector{Int64}:
 1
 2
 3

julia> b = @. x - 3 * x^2
3-element Vector{Int64}:
  -2
 -10
 -24

julia> b = @. x -3 * x^2
ERROR: LoadError: MethodError: no method matching var"@__dot__"(::LineNumberNode, ::Module, ::Symbol, ::Expr)
The function `@__dot__` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  var"@__dot__"(::LineNumberNode, ::Module, ::Any)
   @ Base broadcast.jl:1303

Julia 1.11.3

1 Like

That seems like a bug. There’s only one method for @., which expects a single expression. There should probably be another method that accepts multiple expressions and concatenates them together. (Macro parsing turns x -3 * x^2 into two expressions, x and -3 * x^2.)

It works if you call the macro with parentheses:

julia> x = [1, 2, 3]
3-element Vector{Int64}:
 1
 2
 3

julia> @.(x -3 * x^2)
3-element Vector{Int64}:
  -2
 -10
 -24

EDIT:
I just looked at the docstring for @.. It’s technically not a bug, because the docstring documents the call as @. expr. In other words, the docstring shows that one expression is expected (though it does not emphasize the fact that only one expression is allowed). However, it might be nice if they could make something like @. x -3 * x^2 work.

2 Likes

Yeah, but it’s still pretty surprising that x -3 * x^2 counts as two expressions in the context of a macro call. That said, it’s quite similar to:

julia> b = [x -3 * x^2]
1×2 Matrix{Int64}:
 1  -3
3 Likes

yep, it’s not evidently obvious that the parser treats that as a single expression if a space is used, and as two expressions if there is no space…

No but it’d be nice if someone could document space-sensitive parsing in array and macro syntax · Issue #30096 · JuliaLang/julia

3 Likes