I'm trying to solve this Stack Overflow question* 
Programmatically/Dynamically import modules in Julia*, I thought I almost 
got the right answer (I got half of it), you can se my answer here: 

http://stackoverflow.com/questions/27696356/programmatically-dynamically-import-modules-in-julia/27712752#27712752

This is the *DynamicImport *module I'm using: 

module DynamicImport

export @dynamic_import, needed_modules


macro dynamic_import(modules)
    (modules = eval(modules))::Vector{Symbol}
    ex = Expr(:toplevel)
    for m in modules
        push!(ex.args, Expr(:import, m))
    end
    return ex
end

function needed_modules(rx::Regex, dir::String=".")
    module_files = filter(rx, readdir(dir))
    module_syms = map(m -> symbol(split(m, '.')[1]), module_files)
end


end




And this is what happens, and I still can't figure it out (hygiene?):

julia> using DynamicImport

julia> @dynamic_import [:mod01, :mod02]

julia> rx = r"^mod[0-9][0-9].jl$";

julia> @dynamic_import needed_modules(rx)
ERROR: rx not defined

julia> modules = needed_modules(rx)
2-element Array{Symbol,1}:
 :mod01
 :mod02

julia> @dynamic_import modules
ERROR: modules not defined


But if I define both the macro, the function and the regex in the REPL it 
works:

julia> macro dynamic_import(modules)
           (modules = eval(modules))::Vector{Symbol}
           ex = Expr(:toplevel)
           for m in modules
               push!(ex.args, Expr(:import, m))
           end
           return ex
       end

julia> function needed_modules(rx::Regex, dir::String=".")
           module_files = filter(rx, readdir(dir))
           module_syms = map(m -> symbol(split(m, '.')[1]), module_files)
       end
needed_modules (generic function with 2 methods)

julia> @dynamic_import [:Mod01, :Mod02]    # :vcat expression

julia> rx = r"^Mod[0-9][0-9].jl$";

julia> @dynamic_import needed_modules(rx)    # :call expression

julia> modules = needed_modules(rx)
2-element Array{Symbol,1}:
 :Mod01
 :Mod02

julia> @dynamic_import modules    # Symbol


Thanks in advance! :)






Reply via email to