将字符串内插值传递给宏

day*_*ter 4 macros julia

我对这段代码的问题是当传递内插字符串时宏失败:

macro t(a, b, c)
    quote
        println(:c, " = " , $c)
        ($a, $b, $c)
    end
end

function test()
    # works:
    @t 1 2 3

    # doesn't work:
    x = ?
    @t 1 2 "x is $x"
end

test()
Run Code Online (Sandbox Code Playgroud)
test()
c = 3   # works
ERROR: UndefVarError: x not defined
Run Code Online (Sandbox Code Playgroud)

所以内t插值x是不可用的。有没有解决方案,或者在这里使用宏只是一个坏主意?

Bog*_*ski 5

为了实现您想要的,您需要使用esc

macro t(a, b, c)
    quote
        local a1 = $(esc(a))
        local b1 = $(esc(b))
        local c1 = $(esc(c))
        println(:c, " = ", c1)
        (a1, b1, c1)
    end
end
Run Code Online (Sandbox Code Playgroud)

请注意,我定义了变量a1b1c1一次,然后重用它们。原因是,如果你写了这样的东西:

macro t(a, b, c)
    quote
        println(:c, " = ", $(esc(c)))
        ($(esc(a)), $(esc(b)), $(esc(c)))
    end
end
Run Code Online (Sandbox Code Playgroud)

这是很自然的(或者可能不是:)),你会遇到问题,因为c会被评估两次,例如。在这个例子中:

julia> macro t(a, b, c)
           quote
               println(:c, " = ", $(esc(c)))
               ($(esc(a)), $(esc(b)), $(esc(c)))
           end
       end
@t (macro with 1 method)

julia> function test()
           @t 1 2 rand()
       end
test (generic function with 1 method)

julia> test()
c = 0.03771143425073453
(1, 2, 0.1819496773810383)
Run Code Online (Sandbox Code Playgroud)

请注意,将打印不同的值并返回不同的值。这个问题存在于您的原始宏中(即使它使用了@Bill 指出的全局变量):

julia> macro t(a, b, c)
           quote
               println(:c, " = " , $c)
               ($a, $b, $c)
           end
       end
@t (macro with 1 method)

julia> @t 1 2 rand()
c = 0.7021554643798531
(1, 2, 0.6363717837673994)
Run Code Online (Sandbox Code Playgroud)

一般来说,我认为当您使用元编程调试代码时@code_lowered@macroexpand宏对您很有用。