`Base.@show` macro breaks operator precedence

I just stumbled over the following problem using @show:

julia> @show a=4/2+1;
a = 4 / 2 + 1 = 3.0

julia> @show a=4/2 +1;
a = 4 / 2 = 2.0
1 = 1

julia> a
2.0

julia> @show a=(4/2 +1);
a = 4 / 2 + 1 = 3.0

julia> a
3.0

In the second example the space obviously split the expression into two, which I did not expect and I also did not recognise as such at first. This caused me quite some trouble during debugging, since I did not expect @show to essentially alter my code. You could argue that I should not have put a space, but in my case the space was even unavoidable, since I used something like this

julia> @show a=(4,3).÷2 .+1;
a = (4, 3) .÷ 2 = (2, 1)
(.+)(1) = 1

I am using Julia V1.8.2.

I think this is an inherent issue with how macros split arguments. Try adding parenthesis:

  • @show( a=4/2 +1 )
  • @show (a=4/2 +1)
2 Likes

Yes. I also found this is an option, yet for a quick debugging its a little annoying to keep thinking about added brackets. But I can see that this is a general issue with macro argument splitting.