Reputation: 19162
I want to build a constructor with keyword arguments inside of a macro, and the first keyword argument needs to be for an expression. I am having trouble putting that expression into the expression. Here's what I mean. Say I have a type
type Test
ex
end
which holds an expression. I want to make a constructor where origex = :(a * b)
is the default from a keyword argument. I tried
@eval :(Test(ex=$origex) = Test(origex))
But if you look at the expression that makes:
Test(ex=a * b) = begin # console, line 1:
Test(origex)
end
you see that it won't work because the a*b
needs to still be an expression. So I tried
@eval :(Test(ex=:($origex)) = Test(origex))
but this has the odd expression
Test(ex=$(Expr(:quote, :($(Expr(:$, :origex)))))) = begin # console, line 1:
Test(origex)
end
which also won't eval
. Instead I need to get
Test(ex=:(a * b)) = begin # console, line 1:
Test(origex)
end
as the expression to eval, but I don't know how to get that expression into an expression.
Upvotes: 4
Views: 125
Reputation: 5325
I think the following is what you want. You seem to have had a few mistakes:
julia> type Test
ex::Expr
end
julia> orig_ex = :(a + b)
:(a + b)
julia> new_ex = Meta.quot(orig_ex)
:($(Expr(:quote, :(a + b))))
julia> code = :( Test(; ex=$new_ex) = Test(ex) )
:(Test(; ex=$(Expr(:quote, :(a + b)))) = begin # REPL[4], line 1:
Test(ex)
end)
julia> eval(code)
Test
julia> Test()
Test(:(a + b))
Upvotes: 6