I'd like to write a simple macro that shows the names & values of variables. In Common Lisp it would be
(defmacro dprint (&rest vars)
`(progn
,@(loop for v in vars
collect `(format t "~a: ~a~%" ',v ,v))))
In Julia I had two problems writing this:
- How can I collect the generated
Expr
objects into a block? (In Lisp, this is done by splicing the list with,@
intoprogn
.) The best I could come up with is to create anExpr(:block)
, and set itsargs
to the list, but this is far from elegant. - I need to use both the name and the value of the variable. Interpolation inside strings and quoted expressions both use
$
, which complicates the issue, but even if I usestring
for concatenation, I can 't print the variable's name - at least:($v)
does not do the same as',v
in CL...
My current macro looks like this:
macro dprint(vars...)
ex = Expr(:block)
ex.args = [:(println(string(:($v), " = ", $v))) for v in vars]
ex
end
Looking at a macroexpansion shows the problem:
julia> macroexpand(:(@dprint x y))
quote
println(string(v," = ",x))
println(string(v," = ",y))
end
I would like to get
quote
println(string(:x," = ",x))
println(string(:y," = ",y))
end
Any hints?
EDIT: Combining the answers, the solution seems to be the following:
macro dprint(vars...)
quote
$([:(println(string($(Meta.quot(v)), " = ", $v))) for v in vars]...)
end
end
... i.e., using $(Meta.quot(v))
to the effect of ',v
, and $(expr...)
for ,@expr
. Thank you again!