Quote-unquote idiom in Julia & concatenating Expr objects
<p>I'd like to write a simple macro that shows the names &amp; values of variables. In Common Lisp it would be</p>

<pre><code>(defmacro dprint (&amp;rest vars)
  `(progn
    ,@(loop for v in vars
            collect `(format t "~a: ~a~%" ',v ,v))))
</code></pre>

<p>In Julia I had two problems writing this:</p>

<ol>
<li>How can I collect the generated <code>Expr</code> objects into a block? (In Lisp, this is done by splicing the list with <code>,@</code> into <code>progn</code>.) The best I could come up with is to create an <code>Expr(:block)</code>, and set its <code>args</code> to the list, but this is far from elegant.</li>
<li>I need to use both the name and the value of the variable. Interpolation inside strings and quoted expressions both use <code>$</code>, which complicates the issue, but even if I use <code>string</code> for concatenation, I can 't print the variable's name - at least <code>:($v)</code> does not do the same as <code>',v</code> in CL...</li>
</ol>

<p>My current macro looks like this:</p>

<pre><code>macro dprint(vars...)
  ex = Expr(:block)
  ex.args = [:(println(string(:($v), " = ", $v))) for v in vars]
  ex
end
</code></pre>

<p>Looking at a macroexpansion shows the problem:</p>

<pre><code>julia&gt; macroexpand(:(@dprint x y))
quote 
    println(string(v," = ",x))
    println(string(v," = ",y))
end
</code></pre>

<p>I would like to get</p>

<pre><code>quote 
    println(string(:x," = ",x))
    println(string(:y," = ",y))
end
</code></pre>

<p>Any hints?</p>

<p>EDIT: Combining the answers, the solution seems to be the following:</p>

<pre><code>macro dprint(vars...)
  quote
    $([:(println(string($(Meta.quot(v)), " = ", $v))) for v in vars]...)
  end
end
</code></pre>

<p>... i.e., using <code>$(Meta.quot(v))</code> to the effect of <code>',v</code>, and <code>$(expr...)</code> for <code>,@expr</code>. Thank you again!</p>