Šimon Mandlík
Šimon Mandlík

Reputation: 303

Julia metaprogramming with using/imports

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?

Upvotes: 3

Views: 103

Answers (1)

hckr
hckr

Reputation: 5583

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)

Upvotes: 6

Related Questions