Exporting symbols of imported module


Suppose I write a module A that imports another module B but I want users that do using A to also have access of symbols exported from B.
If I do

module B
import A
export A.x

then I get

ERROR: LoadError: syntax: extra token "." after end of expression

Is there any workaround ?

See https://github.com/JuliaOpt/JuMP.jl/issues/1761#issuecomment-454301357 for the context.



julia> module A
       foo() = 42

julia> module B
       import ..A   # Would be simply `import A` if A were in the load path rather than in Main
       const foo = A.foo
       export foo

julia> using .B

julia> foo()

Note that this is better than the alternative of import A: foo; export foo because it allows tab completion of A.<TAB> to work. (A binding must not be marked as imported-from-another-module for tab completion to consider it.)


Thank you for your answer. However, I would like to export all symbols exported by A (like what could be done by @reexport import A if Reexport were to support this syntax). With your solution, this would mean doing import A; const foo = A.foo for all symbols exported by A which is equivalent to using A. Is it possible to do it without making the symbols of A accessible inside module B ?


Not that I’m aware of.

Why do you want to do this? It seems a little odd to decide that the user wants those symbols after doing using B, even though they’re never available within B.


For a JuMP extension like SumOfSquares, we would like to user to only have to do using SumOfSquares and not using SumOfSquares, JuMP hence we are doing @reexport using JuMP (see https://github.com/JuliaOpt/SumOfSquares.jl/pull/68).
However, we would like to recommend all non-“throw-away script” to do import JuMP instead of using JuMP (see https://github.com/JuliaOpt/JuMP.jl/pull/1778).
Hence we would like to do import JuMP in JuMP extensions but also reexport all JuMP symbols (see https://github.com/JuliaOpt/JuMP.jl/issues/1761#issuecomment-454301357).


Looking at the code, I think there’s no way to “export without import” because using brings in bindings from the source module which have the exportp flag set:

seems reasonable enough.