1
votes

It looks like Julia v0.6 broke some functionality which I would like to recover.

Suppose I have the macro, struct and function:

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)

This used to work, but now I get the error:

ERROR: UndefVarError: myfoo not defined

How can I recover this functionality in Julia v0.6?

2
works fine here, what's your versioninfo? - Gnimuc
@Gnimuc, whoops, needed to use a different variable name so it didn't recognize it as a global variable, now it causes the error. - Thoth

2 Answers

2
votes

Yeah, so based on Gnimuc's code, if you write your macro like this:

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

See here for a discussion on why this is required: https://github.com/JuliaLang/julia/issues/15085

1
votes

I can't find any change notes corresponding to this, but a quick fix could be:

# 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