Invalid redefinition error for parametric type defined inside macro call

Hi folks, been using Julia happily on and off since v0.2, here is a thing I keep stumbling over:

julia> macro passthru(ex)
       ex
       end
@passthru (macro with 1 method)

julia> @passthru type MyType{T}
              args::Vector{T}
              end

julia> @passthru type MyType{T}
              args::Vector{T}
              end
ERROR: invalid redefinition of constant MyType

The same thing works fine with non-parametric types:

julia> @passthru type MyType2
              args::Vector
              end

julia> @passthru type MyType2
              args::Vector
              end

In words, when I create parametric type inside a macro several times, Julia fails to detect that the redefinition agrees with the prior definition and throws an error.
OTOH without the macro call this works fine:

julia> type MyType3{T}
              args::Vector{T}
              end

julia> type MyType3{T}
              args::Vector{T}
              end

Is this a bug or am I misunderstanding something?

Look at the result of macroexpand. You likely need to escape the input expression.

julia> macro passthru(ex)
           esc(ex)
       end

julia> @passthru type MyType{T}
           args::Vector{T}
       end

julia> @passthru type MyType{T}
           args::Vector{T}
       end
2 Likes