Reputation: 1731
As I am trying to understand metaprogramming in Julia: What is missing in this code, respectively, how can I call the proper constructor of Tuple
?
x = (:a, :b)
p = quote
f_a = 3
f_b = 4
y = Tuple($([Symbol("f_", k) for k in x]...))
end
This will generate me the following code:
quote
f_a = 3
f_b = 4
y = Tuple(f_a, f_b)
end
This is of course wrong, because Tuple has no appropriate constructor. I would like to have y
being a Tuple in the end, but I don't see yet how to get the additional parentheses.
In other words, what is missing in this code:
x = (:a, :b)
p = quote
f_a = 3
f_b = 4
y = ($([Symbol("f_", k) for k in x]...))
end
eval(p)
@assert isa(y, Tuple)
Upvotes: 1
Views: 199
Reputation: 10127
You can put a comma,
x = (:a, :b)
p = quote
f_a = 3
f_b = 4
y = ($([Symbol("f_", k) for k in x]...),) # added a comma here
end
eval(p)
@assert isa(y, Tuple)
The following might be instructive
julia> (3)
3
julia> (3,)
(3,)
julia> typeof(ans)
Tuple{Int64}
Upvotes: 2