Problems about using modules in same file

The fundamental purpose of modules is to isolate sets of names from each other like this, in other words modules are namespaces. Imports are how different modules share names with each other (in the case of packages, the first import in a session also irreversibly loads package code, then the following imports just share names again). In a typical module block, the only automatic import you have is using Base, and you’ll need to do every other import manually. If you don’t want to do that, then you probably don’t want separate modules.

You might still be wondering; why can’t modules just automatically import their own names in each other? After all, the other names in the module are still isolated, so shouldn’t that be close enough? That can’t happen because namespaces also isolate distinct names with the same symbols, so without imports, a Cells.differentiate is completely different from Calculus.differentiate. To evade the error in your example another way, I can make an entirely separate module also named moduleA nested inside moduleB to execute different code:

julia> module  moduleB
           module moduleA
               function f1()
                   println("the wrong one")
               end
           end
           function f2()
               moduleA.f1()
           end
       end
Main.moduleB

julia> moduleB.f2()
the wrong one

julia> moduleA.f1()
test

julia> moduleA === moduleB.moduleA
false
2 Likes