Search code examples
modulejuliausing

`using` an inner module with its symbol, in a nested structure of modules


I have a nested structure of modules like this:

module TestMod
  module B
    export BB
    module BB

    end
  end
  module C
    module D
      #using ...B
      importall ...B 
      using BB # => ERROR: ArgumentError: Module BB not found in current path.
    end
  end
end

I want to do using BB in module D but it seems that the only way is to write a full path for BB (e.g. using B.BB), both import or using do not help.


Solution

  • Once you've usinged B, you can do a relative import from the current module to any exported modules of B, including BB. See

    julia> module TestMod
             module B
               export BB
               module BB
                 x = 2
                 export x
               end
             end
             module C
               module D
                 using ...B 
                 using .BB
                 println(x)
               end
             end
           end
    2
    TestMod
    

    The syntax using .BB means to use the module with name BB in the current module, whereas using BB means using top-level module BB; that is, it will look for Main.BB, which is not what you want.