Exporting symbols of imported module


#1

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
end

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.


#2

Yes:

julia> module A
       foo() = 42
       end
Main.A

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
       end
Main.B

julia> using .B

julia> foo()
42

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.)


#3

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 ?


#4

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.


#5

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).


#6

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.