Differences in these expressions?

When learning metaprogramming in Julia, I am a little bit confusing about these behavior like below

julia> Meta.show_sexpr(:(:($(1+2))))
(:quote,(:$,(:call,:+,1,2)))
julia> Meta.show_sexpr(:($(1+2)))
3
Julia> Meta.show_sexpr(Expr(:$,:(1+2)))
(:$,(:call,:+,1,2))

As in the first case, part of

:(:($(1+2)))

is (which should be the same as in the third expression?)

:($(1+2))

And I expect its behavior is the same as:

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

But it obviously doesn’t support my expectation.According to the manual, it says the construction using Expr() and :() should be the same, but why are they different in such a case?
Just because the construction using :() will be default interpolated

:($(1+2))

But the construction using Expr() will not be interpolated?

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

Or any other reasons?

Which part of the manual? There’s no such guarantee. :() is not the syntax for creating expressions, it’s the syntax for quoting. There are many other cases where :() does not even create Expr objects so the two cannot possibly be equivalent.

There’s also no guarentee that there’s a quoting syntax to create all possibly valid expressions.

Just the manual said:(In the section Quoting)

The second syntactic purpose of the : character is to create expression objects without using the explicit Expr constructor. This is referred to as quoting . The : character, followed by paired parentheses around a single statement of Julia code, produces an Expr object based on the enclosed code

He said that “The : character, followed by paired parentheses around a single statement of Julia code, produces an Expr object”.
I don’t know whether the statement is saying :() is equivalent to Expr()?

Or the real problem is that

$(1+2)

isn’t a valid Julia code?

I agree that doc can be confusing, and you are certainly not the first one confused by it. You are certainly welcome to improve it though bear in mind that it also needs to be understandable for people reading it the first time and doesn’t want to worry about all the details yet…

OTOH, I do believe it is correct, in that it more or less implies : can be used to create corresponding Expr but it doesn’t imply the mapping is complete. In that sense, yes, the reason your expectation break down is that $(1 + 2) isn’t a valid expression for you to quote.

2 Likes

Yes, especially in the section of metaprogramming which might be very confusing for me, when I first read the manual :sob:

1 Like

You asked the same question yesterday in

Please don’t open new topics for the same question, especially if you got an answer (if the answer is not satisfactory, just say so and hopefully others will respond and clarify).

OK.
I am sorry to break the rules of community, and I will try my best to understand the manual as far as possible, when I have a confusing question about the explanation in the manual.
Sorry again!