Often when you are debugging and testing a package it can be useful to be able to access all names in a module (whether they are exported or not), without having to prefix each identifier by the module name. It quickly becomes tiresome to have to prefix the module name for each identifier in the REPL, with code that looks like:
julia> import MyModule
julia> MyModule.ConstantA + MyModule.foo(2) + MyModule.bar("hi")
To save yourself from needlessly typing MyModule
, the following @importall
macro takes care of importing into the current namespace all the identifiers from a module, thereby allowing you to instead write:
julia> @importall MyModule
julia> ConstantA + foo(2) + bar("hi")
Saving yourself from countless typing MyModule
.
Here’s the macro definition (inspired from this package altough they do different things):
macro importall(mod)
quote
try
$(esc(mod))
catch
import $(mod)
end
for name in names($(esc(mod)), all=true)
if name ∉ (:eval, :include, :__init__)
@eval import .$(mod): $(Expr(:$, :name))
end
end
end
end
I’ve placed this in my ~/.julia/config/startup.jl
file.
I’ve found this particularly time saving when working with external libraries and testing code in the REPL and debugging tasks.
A less convenient way
Another way to go about the same thing, which I find significantly less REPL friendly, is to wrap the code in an @eval
block, as follows:
julia> @eval MyModule begin
ConstantA + foo(2) + bar("hi")
end