Why they are not equivalent?

In that case, you are constructing this and don’t evaluate it. Of course they are different, you can check this by printing:

julia> Expr(:$,:(1+2))
:($(Expr(:$, :(1 + 2))))

julia> dump(ans)
Expr
  head: Symbol $
  args: Array{Any}((1,))
    1: Expr
      head: Symbol call
      args: Array{Any}((3,))
        1: Symbol +
        2: Int64 1
        3: Int64 2

It is not clear what you are trying to do, but perhaps you are looking for

julia> ex = Meta.quot(Expr(:$, :(1 + 2)))
:($(Expr(:quote, :($(Expr(:$, :(1 + 2)))))))

julia> eval(ex)
3

You may also be interested in

https://docs.julialang.org/en/v1/devdocs/ast/

2 Likes