Why they are not equivalent?

A try like below:

Expr(:$,:(1+2)) == :($(1+2))

gives false.
Why they are not equivalent?
I suppose the righthand side has been evaluated to 3. Is this the real reason?
Thanks!

The $ is special syntax for interpolation:

https://docs.julialang.org/en/v1/manual/metaprogramming/#man-expression-interpolation-1

so the RHS is essentially 3, ie

julia> :($(1+2))
3

Yes, I suppose it should be the reason. But the lefthand expression’s head is also $, are they different?
Or what’s real form of the lefthand expression like the righthandside using :() forms?

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

So the difference is that the lefthand side is just a construction, however the righthand side has been evaluated using interpolation.

Also note the Meta.quot. And technically interpolation is part of the evaluation semantics, so everything is