colinfang
colinfang

Reputation: 21757

Does macro hygiene only protect you in different modules?

As for Julia 0.4.2

macro g(y)
    :((x, $y, $(esc(y))))
end

x = 1
function t()
    x = 2
    println(macroexpand(:(@g(x))))
    println(@g(x))
end

t()
println(x)

>>>
(x,x,x)
(2,2,2)
1

I had expected the result to be (1, 1, 2).

However, if I define the macro in a different module, it works as expected.

module A
macro g(y)
    :((x, $y, $(esc(y))))
end
x = 1
end

x = 3

function t()
    x = 2
    println(macroexpand(:(A.@g(x))))
    println(A.@g(x))
end

t()

>>>
(A.x,A.x,x)
(1,1,2)

Seems the hygiene simply prefix the symbols with the module namespace. Therefore it is impossible for the macro expander to distinguish different scopes in the first case.

Is this the intended behaviour?

Upvotes: 4

Views: 114

Answers (1)

Toivo Henningsson
Toivo Henningsson

Reputation: 2709

Seems like a bug. Please report it at Julia's github page.

Upvotes: 1

Related Questions