It is confusing for me when I use Meta.parse() function as:
Julia> x = 2
Julia> Meta.parse(":(:(v = $x))")
:($(Expr(:quote,:($(Expr(quote,:(v = 2)))))))
In general, an interpolation with single $ in :(:($x)) should not interpolate the value of x (which is 2 in the above example).
Since the manual doesn’t mention the case, is this rule special to Meta.parse() function?
It’s just doing string interpolation before Meta.parse is called at all, and doesn’t care what the string contains:
julia> x = 2;
julia> ":(:( :) v = $x )])" # nonsense
":(:( :) v = 2 )])"
ERROR: Base.Meta.ParseError("missing comma or ) in argument list")
Oh, you are right, I have totally forgotten the string interpolation!
By the way, is there any method to use Meta.parse to parse such form as:
":(:(v = $x))"
to see how Julia transform into internal AST form?
The issue is on string, so you just need to produce the correct string. You can escape
You might also be interested in the
Meta.@dump macro, which prints the AST you give it, so you could do e.g.
Meta.@dump :(v = $x) to inspect the AST. If you want to know how these are actually generated internally, I’m afraid, you will have to learn Lisp.