3

I would like to evaluate an expression that imports a Module, based on an argument expr. So far, I came up with:

julia> expr = :(Base.Threads)

julia> @eval using $expr
ERROR: TypeError: import or using: expected Symbol, got Expr
Stacktrace:
 [1] eval(::Module, ::Expr) at ./sysimg.jl:23

One possibility is to use Expr constructor directly, like this:

julia> expr = [:Base, :Threads]
2-element Array{Symbol,1}:
 :Base   
 :Threads

julia> eval(Expr(:using, expr...))

But is there any other, perhaps more straightforward way without the need of constructing Expr?

Šimon Mandlík
  • 303
  • 1
  • 6

1 Answers1

6

Each space delimited character group after the macro name is considered to be a separate argument. Instead, you should just write the expression in between parentheses.

@eval(using $expr)
hckr
  • 5,456
  • 1
  • 20
  • 31