dhoegh / BuildExecutable.jl

Build a standalone executables from a Julia script
Other
156 stars 21 forks source link

@generated @nloops #16

Closed bjarthur closed 8 years ago

bjarthur commented 8 years ago

when using @generated in conjunction with @nloops, i get an UndefVarError(symbol("@nloops")) error when the built executable is run on the command line. the code runs fine when (a) either macro is used alone, or (b) the source is evaluated in the REPL. any ideas? see code below.

importall Base.Cartesian

# this does NOT work
@generated function foo{T,N}(A::Array{T,N})
  println(N)
  quote
    @nloops $N i A begin
      println(@nref($N,A,i))
    end
  end
end

#= but this DOES work
@generated function foo(A)
  println(A)
  quote
    println(A)
  end  
end    

# and so DOES this
function foo(A)
  @nloops 3 i A begin
    println(@nref(3,A,i))
  end
end
=#

function main()
  println("hello world!")
  foo(rand(3,3,3))
end

this blog seems relevant, but i don't entirely understand it.

dhoegh commented 8 years ago

This is due to scoping of the module, when compiling the script. If you shall have the same experience in the REPL then you can only use the import statment. See https://github.com/dhoegh/BuildExecutable.jl/blob/master/test/test.jl where JSON is imported. Even if changing the import to using the functions from the JSON package should still be qualified with JSON.