Print expressions with ()s inserted for visualizing precedence



Sometimes I forget precedence rules and would like to see how an expression is parsed as a reminder. Eg if I have

ex = parse("x isa Float64 || return false")

then it would be nice to display it as

(x isa Float64) || (return false)

I know about dump and Base.show_sexpr, but they are either too verbose or require switching mentally between M and S expressions. Is there a way I can trick Base functions into doing what I want?


I think it’s a hard-coded behavior in Base to only show the parens if necessary to disambiguate precedence:


I’m not sure what the best API for requesting more parens is, but if you figure out a good one a PR would be welcomed. There’s also the alternative of just printing S-expressions:

julia> Meta.show_sexpr(:(x isa Float64 || return false))
(:||, (:call, :isa, :x, :Float64), (:return, false))