# Making types available across all submodules

**URL:** <https://discourse.julialang.org/t/making-types-available-across-all-submodules/87125>\
**Category:** General Usage\
**Tags:** question, modules\
**Created:** [September 12, 2022, 10:23am UTC](https://discourse.julialang.org/t/making-types-available-across-all-submodules/87125 "2022-09-12T10:23:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tharsalys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tharsalys/32/38414_2.png) [@Tharsalys](https://discourse.julialang.org/u/Tharsalys)\
**Post date:** [September 12, 2022, 10:23am UTC](https://discourse.julialang.org/t/making-types-available-across-all-submodules/87125/1 "2022-09-12T10:23:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 12, 2022, 10:47am UTC](https://discourse.julialang.org/t/making-types-available-across-all-submodules/87125/2 "2022-09-12T10:47:58Z")

</div>

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

```julia
using ..MainModule

```

---

<div class="post-metadata">

**Author:** ![Tharsalys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tharsalys/32/38414_2.png) [@Tharsalys](https://discourse.julialang.org/u/Tharsalys)\
**Post date:** [September 12, 2022, 10:49am UTC](https://discourse.julialang.org/t/making-types-available-across-all-submodules/87125/3 "2022-09-12T10:49:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 12, 2022, 10:50am UTC](https://discourse.julialang.org/t/making-types-available-across-all-submodules/87125/4 "2022-09-12T10:50:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tharsalys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tharsalys/32/38414_2.png) [@Tharsalys](https://discourse.julialang.org/u/Tharsalys)\
**Post date:** [September 12, 2022, 10:57am UTC](https://discourse.julialang.org/t/making-types-available-across-all-submodules/87125/5 "2022-09-12T10:57:10Z")

</div>

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:

```julia
# 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:

```julia
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

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 12, 2022, 11:02am UTC](https://discourse.julialang.org/t/making-types-available-across-all-submodules/87125/6 "2022-09-12T11:02:08Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![Tharsalys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tharsalys/32/38414_2.png) [@Tharsalys](https://discourse.julialang.org/u/Tharsalys)\
**Post date:** [September 12, 2022, 11:05am UTC](https://discourse.julialang.org/t/making-types-available-across-all-submodules/87125/7 "2022-09-12T11:05:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 12, 2022, 11:05am UTC](https://discourse.julialang.org/t/making-types-available-across-all-submodules/87125/8 "2022-09-12T11:05:47Z")

</div>

Yes, like if it was a directory tree.
