Making types available across all submodules

I have a main module and some submodules. Right now, there’s a types.jl in the main module whose types are also required in the submodules. But naturally I can’t make these types accessible in the submodules without including types.jl in each submodule, which redefines the type for those modules and then the method dispatch doesn’t work properly – A.B.SomeType not the same as A.SomeType, etc.

I figure my usage of types.jl is wrong and each module is supposed to have its own types.jl. So I’m thinking of moving all the types in their respective modules where they are required but, that may lead to circular dependencies among submodules.

Basically, is there a way to declare types in the main module and somehow make them accessible in the submodules as well without circular dependencies?

Use the main module in the submodules, i.e.

using ..MainModule
1 Like

That’s a circular dependency too cuz I need to use the submodules in the main module.

Then define your types in a separate module and use that module all around. (Include it only once at the top level)

I thought of that solution but wouldn’t that lead to the same problem? I think I did a poor job at explaining the original post so let me elaborate:

# defined in submoduleA.jl
module SubModuleA

include("../types.jl")
using .Types: SomeType

# do stuff
function somefuncinA(::SomeType) end 
end

# defined in submoduleB.jl
module SubModuleB 
include("../types.jl")
using .Types: SomeType

# do stuff
function somefuncinB(::SomeType) end 
end 

And then, in the main module:

module MainModule 
include("submodules/submoduleA.jl")
using .SubModuleA

include("submodules/submoduleB.jl")
using .SubModuleB

# this won't work with somefuncinB because that's defined for B.SomeType 
someobj = SubModuleA.SomeType()

end

What you should not do is to include the same the same file twice. Otherwise it should be fine to use the modules property everywhere. For example:

julia> module MainModule
           # could be "include("typesmodule.jl")
           module TypesModule
               struct A x::Int end
           end

           using .TypesModule

           module Submodule
               export f
               using ..TypesModule: A
               f(x) = A(x) 
           end

           # now you can use Submodule normally
           using .Submodule
           a_in_Main = f(1)

       end

julia> using .MainModule

julia> MainModule.a_in_Main
Main.MainModule.TypesModule.A(1)
1 Like

Thanks! I was missing the fact that I could do ... Does .. mean “look for this module in the parent module”?

Yes, like if it was a directory tree.

1 Like