这个朱莉娅交换出了什么问题!宏?

Wil*_*hes 4 macros julia

我正在尝试swap!在Julia中编写一个简单的宏来理解宏系统.到目前为止,这是我的代码:

macro swap!(x, y)
    quote
        local tmp = $(esc(y))
        $x = $(esc(y))
        $y = tmp
    end
end

a = 1
b = 2

@swap!(a, b)

# prints: a: 1, b: 2
println("a: $a, b: $b")
Run Code Online (Sandbox Code Playgroud)

这样运行没有错误,但实际上并没有更改值.Julia似乎没有一个只扩展宏而不执行它的功能(据我所知),所以这很难调试.

REPL中的等价引用似乎按预期工作:

julia> a = 1
1

julia> a_sym = :a
:a

julia> a_sym
:a

julia> b = 2
2

julia> b_sym = :b
:b

julia> eval(quote
       tmp = $a_sym
       $a_sym = $b
       $b_sym = tmp
       end)
1

julia> a
2

julia> b
1
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

waT*_*eim 7

我想你可能想要像下面这样的东西,但是因卫生问题而搞得一团糟.诀窍是正确逃脱.

macro swap(x,y)
   quote
      local tmp = $(esc(x))
      $(esc(x)) = $(esc(y))
      $(esc(y)) = tmp
    end
end
Run Code Online (Sandbox Code Playgroud)

这就是扩展的样子

julia> macroexpand(quote @swap(x,y) end)
quote  # none, line 1:
    begin  # none, line 3:
        local #189#tmp = x # line 4:
        x = y # line 5:
        y = #189#tmp
    end
end
Run Code Online (Sandbox Code Playgroud)

效果

julia> x
1

julia> y
2

julia> @swap(x,y)

julia> x
2

julia> y
1
Run Code Online (Sandbox Code Playgroud)

相比之下,您的宏正确地在一个赋值中转义y,而不是在其他2个语句中转义,因此设置引入变量的值而不是预期的x和y.

julia> macroexpand(quote @swap!(x,y) end)
quote  # none, line 1:
    begin  # none, line 3:
        local #208#tmp = y # line 4:
        #209#x = y # line 5:
        #210#y = #208#tmp
    end 
end
Run Code Online (Sandbox Code Playgroud)