module contains some function `func` that isn't exported, how would I call `mod00.func()` if I only know the string "mod00"
julia> versioninfo()
Julia Version 0.3.3
Commit b24213b (2014-11-23 20:19 UTC)
Platform Info:
System: Linux (i686-pc-linux-gnu)
CPU: Intel(R) Atom(TM) CPU N570 @ 1.66GHz
WORD_SIZE: 32
BLAS: libblas
LAPACK: liblapack
LIBM: libm
LLVM: libLLVM-3.3
julia> parse("import foo")
:($(Expr(:import, :foo)))
julia> parse("import foo, bar, baz")
:($(Expr(:toplevel, :($(Expr(:import, :foo))), :($(Expr(:import, :bar))), :($(Expr(:import, :baz))))))
julia> macro dynamic_import(modules)
quote
ex = Expr(:toplevel)
names = map(m -> symbol(split(m, '.')[1]), $modules)
for name in names
push!(ex.args, Expr(:import, name))
end
return ex
end
end
julia> @dynamic_import ["Newton.jl", "MyTest.jl"]
:($(Expr(:toplevel, :($(Expr(:import, :Newton))), :($(Expr(:import, :MyTest))))))
julia> @dynamic_import ["Newton.jl", "MyTest.jl"]
:($(Expr(:toplevel, :($(Expr(:import, :Newton))), :($(Expr(:import, :MyTest))))))
julia> eval(ans)
julia> Newton
Newton
julia> MyTest
MyTest
You don’t need the quote ... end block since you’re creating the expression manually using Expr objects.
Removing that and changing $modules to modules.args should work alright I think.
— Mike
julia> macro dynamic_import(modules)
ex = Expr(:toplevel)
names = map(m -> symbol(split(m, '.')[1]), modules.args)
for name in names
push!(ex.args, Expr(:import, name))
end
return ex
end
julia> @dynamic_import ["Newton.jl", "MyTest.jl"]
julia> Newton
Newton
julia> MyTest
MyTest
No prob.
— Mike
You could also generalize this line:
module_files = filter(r"^mod[0-9][0-9].jl$", readdir())
By abstracting it into a function:
julia> module_files(dir=".") = filter(r"^mod[0-9][0-9].jl$", readdir(dir))
module_files (generic function with 2 methods)
So you may use it like this:
julia> @dynamic_import module_files()
Cheers!
Ismael VC