Reputation: 75
I have a variable ex
that represents an Expr, I want to have a function exprwrap
that creates an Expr from it which when evaluated is equal to ex
.
Currently I implement it as follows:
ex = :(my + expr)
"Make an expression that when evaled returns the input ex."
function exprwrap(ex::Expr)
ret = :(:(du + mmy))
ret.args[1] = ex
ret
end
eval(exprwrap(ex)) == ex
Keep in mind that my
and expr
are not defined so :(:($$ex))
does not do the job.
What is a cleaner way to do this?
Upvotes: 2
Views: 343
Reputation: 69829
You can write:
Expr(:quote, x)
or
Expr(:block, ex)
or
:($ex;)
Additionally you could do:
Meta.parse(":($ex)")
which is not simple but shows you how Julia parses ex
when it appears in the source code and you can see that it is the same as Expr(:quote, ex)
.
Similarly yo can check that Meta.parse("($ex;)") == Expr(:block, ex)
.
Upvotes: 3