Julia macro hygiene for vectors

Viewed 95

I just stumbled upon a strange behaviour while working with macros in Julia. I want to escape all elements in a vector and approched this with the map function. When I store the result back in a vector, the escape function is seems not to be evaluated after the macro call, but when the output of the map function gets stored into variables, the expected behaviour can be seen.

Stored in the vector the elements look similar to a single escaped symbol.

input = [:a, :b]
escaped_input = map(x -> esc(x),input)
println(escaped_input) 
>>> Expr[:($(Expr(:escape, :a))), :($(Expr(:escape, :b)))]

println(esc(:a))
>>> :($(Expr(:escape, :a)))

But when the elements are accessed inside a macro in the quote, the escape function seems to not get executed:

a = 1
b = 2

macro escapeVector(testVec...)
    escapedVec = map(x -> esc(x), collect(testVec))
    quote
        map(x -> println(x), $escapedVec)
        println($escapedVec)
    end
end

macro escapeVars(testVec...)
    esc_a, esc_b = map(x -> esc(x), collect(testVec))
    quote
        println($esc_a)
        println($esc_b)
    end
end

@escapeVector a b
@escapeVars a b

This code produces the following output:

>>> $(Expr(:escape, :a))
>>> $(Expr(:escape, :b))
>>> Expr[:($(Expr(:escape, :a))), :($(Expr(:escape, :b)))]
>>> 1
>>> 2

Is there a way to trigger the escape inside the vector, so that the variables can be accessed or do I miss something obvious here?

0 Answers
Related