Problem with using in Submodules

Dear all,
I have a problem with modules.

I have a file with

module Root

include("Branch.jl")
include("OtherBranch.jl")
include("Stalk.jl")

using .Branch, .OtherBranch, .Stalk

end

Now, in OtherBranch.jl I need something which is defined in Stalk, where I have defined a module called StalkDef, so my code is as follows

module OtherBranchDef
using ..StalkDef

end

but it fails to compile, saying that StalkDef is not defined.
I tried also with

module OtherBranchDef
using Root.StalkDef

end

but it is not working.
It seems like it is processing the file from top to bottom, so I would have to move the inclusion of the two files (I cannot, they “use” each other).

How does this work, I think I’m missing something.

Best wishes
Isaia

Yes, that’s right.

That’s the actual problem–you can’t do this. Here’s a simpler non-working example:

julia> module A
         module B
           using ..C
         end
         module C
           using ..B
         end
       end
ERROR: UndefVarError: C not defined

There must be a non-cyclic ordering of your various modules; they cannot depend on each other. If they both need to share some common information (such as a type definition), then you can move the shared information into a new module and have both modules depend on that shared module.

2 Likes

Oh, thank you! I am trying to refactor the code to avoid this…

@rdeits, I pilfered your nice MWE for

https://github.com/JuliaLang/julia/pull/38271

2 Likes

It is maybe worth noting that while you may not do this with two “sibling modules” you can do it with “parent and children”:

julia> module Outer
         module Inner
           import ..Outer
           function i()
             Outer.o()
           end
         end
         import .Inner
         function o()
           Inner.i()
         end
         export o
       end

Which compiles and gives a ERROR: StackOverflowError: if any of the functions is called.