2017-07-15 54 views
1

看起來Julia v0.6破壞了一些我想要恢復的功能。Julia v0.6中破壞的宏功能

假設我有宏觀,結構和功能:

macro juliadots(expr::Expr) 
    expr = :(print_with_color(:red, " ●"); 
       print_with_color(:green, "●"); 
       print_with_color(:blue, "● "); 
       print_with_color(:bold, $expr)) 
    return expr 
end 

struct Foo 
    x::String 
end 

function func(foo) 
    @juliadots "$(foo.x)\n" 
end 

myfoo = Foo("hello") 
func(myfoo) 

這用來工作,但現在我得到的錯誤:

ERROR: UndefVarError: myfoo not defined 

如何恢復在朱莉婭V0此功能。 6?

+0

在這裏工作正常,什麼是您的versioninfo? – Gnimuc

+0

@Gnimuc,哎呀,需要使用一個不同的變量名,所以它不會將它識別爲全局變量,現在它會導致錯誤。 – Thoth

回答

1

我無法找到對應於這個任何改變註釋,但速戰速決可能是:

# Julia-v0.6 
julia> func(foo) = @juliadots :($("$(foo.x)\n")) 
func (generic function with 1 method) 

julia> @macroexpand @juliadots :($("$(foo.x)\n")) 
quote 
    (Main.print_with_color)(:red, " ●") 
    (Main.print_with_color)(:green, "●") 
    (Main.print_with_color)(:blue, "● ") 
    (Main.print_with_color)(:bold, "$(foo.x)\n") 
end 

# Julia-v0.5 
julia> func(foo) = @juliadots "$(foo.x)\n" 
func (generic function with 1 method) 

julia> macroexpand(:(@juliadots "$(foo.x)\n")) 
quote 
    print_with_color(:red," ●") 
    print_with_color(:green,"●") 
    print_with_color(:blue,"● ") 
    print_with_color(:bold,"$(foo.x)\n") 
end 
2

呀,所以基於Gnimuc的代碼,如果你寫你的宏是這樣的:

julia> macro juliadots(ex::Expr) 
    expr = :(print_with_color(:red, " ●"); 
       print_with_color(:green, "●"); 
       print_with_color(:blue, "● "); 
       print_with_color(:bold, :($($(ex))))) 
    return expr 
end 

julia> func(myfoo) 
●●● hello 

請參閱這裏討論爲什麼這是必需的:https://github.com/JuliaLang/julia/issues/15085