2017-07-30 46 views
2

谁能解决我有这个宏的错误,它才开始在0.6版本发生:内部功能朱莉娅V0.6宏观

mutable struct Foo 
    x::Int 
end 

macro test(myfoo) 
    quoteblock = 
    quote 
    myfoo.x += 1 
    end 

    return quoteblock 
end 

function func(myfoo) 
    @test myfoo 
    println(myfoo.x) 
end 

foo = Foo(3) 
func(foo) 

理论上这应该只是代替线上@test myfoo在功能funcmyfoo.x += 1在编译的时候,这应该工作,而是我得到的错误:

UndefVarError: myfoo not defined 

回答

3

相应的变化,票据上市here

When a macro is called in the module in which that macro is defined, global variables in the macro are now correctly resolved in the macro definition environment. Breakage from this change commonly manifests as undefined variable errors that do not occur under 0.5. Fixing such breakage typically requires sprinkling additional escs in the offending macro (#15850).

所以答案是逃避myfoo

macro test(myfoo) 
    quote 
    $(esc(myfoo)).x += 1 
    end 
end 
+0

这解决了我的例子,但显然我的例子并没有在我的实际代码捕获一些微妙的。当我包含$(esc())时,我立即尝试加载我的源代码时立即得到未定义的错误。 – Thoth

+0

@Thoth可以发布整个代码吗? – Gnimuc

+0

nm,我想出了一种适应您的解决方案的方法,谢谢。 – Thoth