Programmatically defining new modules


#1

Is it not possible to programatically define modules?

My naive attempt fails:

julia> quote module Junk end end
quote
    #= REPL[38]:1 =#
    module Junk
    #= REPL[38]:1 =#
    #= REPL[38]:1 =#
    end
end

julia> eval(ans)
ERROR: syntax: "module" expression not at top level
Stacktrace:
 [1] eval at ./boot.jl:319 [inlined]
 [2] eval(::Expr) at ./client.jl:393
 [3] top-level scope at none:0

#2

you could use ‘Module’ constructor and then eval Exprs in it.


#3

Maybe you need to “escape” the module definition in the quote. Just guessing


#4

It seems to work using :( ... ) rather than quote

julia> names(Main)
4-element Array{Symbol,1}:
 :Base
 :Core
 :InteractiveUtils
 :Main

julia> for n in ["Tom", "Dick", "Harry"]
           println(n)
           eval(:(module $(Symbol(n)) test()="Testing $($n)" end))
       end
Tom
Dick
Harry

julia> names(Main)
8-element Array{Symbol,1}:
 :Base
 :Core
 :Dick
 :Harry
 :InteractiveUtils
 :Main
 :Tom
 :ans

julia> Dick.test()
"Testing Dick"

julia> Harry.test()
"Testing Harry"

julia> Tom.test()
"Testing Tom"