Julia v0.6中的宏功能损坏

时间:2017-07-15 06:06:57

标签: julia

看起来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

如何在Julia v0.6中恢复此功能?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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