Macro inside constructor fails on 0.6

The following code works on 0.5, but not on 0.6 (0.6.0-pre.alpha.0)

macro macroex(n)
  :(println($n))
end

type MWE
  nvar :: Int
  x0   :: Array{Float64,1}

  function MWE(nvar)
    x0 = zeros(nvar)
    @macroex nvar # <-- Fails here
    return new(nvar, x0)
  end

end

function foo()
  @macroex 5
  mwe = MWE(5)
  println(mwe.x0)
end

foo()

on line 11 (annotated) with ERROR: LoadError: UndefVarError: nvar not defined.

Any input is greatly appreciated.

Thanks in advance.

You need $(esc(n)) in the macro; see the macro hygiene section of the manual.

3 Likes

Also note that @show is already defined to print the value of a variable.

Thank you very much, it solved my issue.