【发布时间】:2015-10-20 04:57:51
【问题描述】:
我在 Julia here 中找到了一个 unless 宏的示例,如下所示:
macro unless(test, branch)
quote
if !$test
$branch
end
end
end
但是,当我尝试使用它时,它失败了(显然存在卫生问题,但我无法确切地弄清楚)。这是我使用的测试:
x, y = 0, 1
@unless (x == 5) begin # should execute
y = 3
end
@unless (x == 0) begin # should not execute
y = 5
end
@assert y == 3 # FAILS! SAYS y is 0
现在我可以通过转义 分支而不是测试来完成这项工作:
macro unless(test, branch)
quote
if !$test
$(esc(branch))
end
end
end
我的问题是:为什么只逃避分支而不是测试就足够了?现在我确实尝试了宏扩展。在第一种情况下,没有esc,我得到这个:
julia> macroexpand(:(@unless (x == 5) begin y = 3 end))
quote # none, line 3:
if !(x == 5) # none, line 4:
begin # none, line 1:
#2#y = 3
end
end
end
现在即使 没有 宏参数被转义,只有 y 被生成了!谁能解释为什么会这样? (我知道第二个版本有效,因为当我逃离分支时, y 没有得到 gensymed 并且宏按预期扩展为 y = 3。但我完全不知道为什么 x 不是即使没有使用 esc,也可以生成 gensymed。)
【问题讨论】: