2017-07-30 48 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