`@def` macro generator broken on master


#1

I use this @def on v0.5 to generate macros for inlining code.

julia>   macro def(name, definition)
             return quote
                 macro $name()
                     esc($(Expr(:quote, definition)))
                 end
             end
         end
@def (macro with 1 method)

However, on the most current nightly I get an error:

julia> @def test begin
         a = 2
       end
ERROR: syntax: invalid macro definition

Can someone point me to what changed and what the workaround is?


#2

Escape all user inputs.


#3

I see, name wasn’t escaped:

macro def(name, definition)
    return quote
        macro $(esc(name))()
            esc($(Expr(:quote, definition)))
        end
    end
end
@def test begin
         a = 2
       end

# Now @test "pastes" the code `a=2`

is good now.


UndefVarError when including a snippet of code
Include a script from external file in different scopes