Interpolation of expression into expression inside quote

This question builds the previous SO question , which was intended to build expressions from expressions inside a macro. Nevertheless, everything turned out to be a small trucker when quoting the entire expression. For example, I want to build an expression: (name = val). Following:

macro quotetest(name,val) quote nm = Meta.quot($(QuoteNode(name))) v = Meta.quot($(QuoteNode(val))) println(nm); println(typeof(nm)) println(v); println(typeof(val)) end end @quotetest x 5 # Test case: build :(x=5) 

displays

 :x Expr $(Expr(:quote, 5)) Expr 

showing that I'm on the right track: nm and val are the expressions I want inside the quote. However, at the moment I can not apply the previous solution. For instance,

 macro quotetest(name,val) quote nm = Meta.quot($(QuoteNode(name))) v = Meta.quot($(QuoteNode(val))) println(nm); println(typeof(nm)) println(v); println(typeof(v)) println(:($(Expr(:(=),$(QuoteNode(nm)),$(QuoteNode(val)))))) end end 

doesn't work saying nm is not defined . I tried just interpolating without QuoteNode , avoiding interpolating $(esc(nm)) , etc. I can't figure out how to get this to build an expression.

+5
source share
1 answer

I think you use the $ signs more than you need. Is this what you are looking for?

 julia> macro quotetest(name,val) quote expr = :($$(QuoteNode(name)) = $$(QuoteNode(val))) println(expr) display(expr) println(typeof(expr)) end end @quotetest (macro with 1 method) julia> @quotetest test 1 test = 1 :(test = 1) Expr 
+4
source

Source: https://habr.com/ru/post/1257599/


All Articles