Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 491
Šimon Mandlík Avatar asked Dec 01 '25 22:12

Šimon Mandlík


1 Answers

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)
like image 104
hckr Avatar answered Dec 04 '25 21:12

hckr