# How to let two modules use part of each other?

**URL:** https://discourse.julialang.org/t/how-to-let-two-modules-use-part-of-each-other/17075
**Category:** General Usage
**Created:** [November 1, 2018, 11:26pm UTC](https://discourse.julialang.org/t/how-to-let-two-modules-use-part-of-each-other/17075 "2018-11-01T23:26:53Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)
#### Post date: [November 1, 2018, 11:26pm UTC](https://discourse.julialang.org/t/how-to-let-two-modules-use-part-of-each-other/17075/1 "2018-11-01T23:26:53Z")

</div>

For instance:

```julia
module moduleA

using moduleB

export StructA

mutable struct StructA 
	i::Int
	SB::StructB
end

end

```

```julia
module moduleB

# import moduleA.StructA
using moduleA: StructA

export StructB
export func

mutable struct StructB
	i::Int
end

function func(A::StructA)
	A.i += 1
end

end

```

The above does not work:

```julia
julia> using moduleA
[Info: Precompiling moduleA [top-level]
┌ Warning: Module moduleA with build ID 89941935020118 is missing from the cache.
│ This may mean moduleA [top-level] does not support precompilation but is imported by a module that does.
└ @ Base loading.jl:944
ERROR: LoadError: LoadError: UndefVarError: StructA not defined
Stacktrace:
 [1] include at ./boot.jl:317 [inlined]
 [2] include_relative(::Module, ::String) at ./loading.jl:1041
 [3] _require(::Base.PkgId) at ./loading.jl:953
 [4] require(::Base.PkgId) at ./loading.jl:855
 [5] macro expansion at ./logging.jl:311 [inlined]
 [6] require(::Module, ::Symbol) at ./loading.jl:837
 [7] include at ./boot.jl:317 [inlined]
 [8] include_relative(::Module, ::String) at ./loading.jl:1041
 [9] include(::Module, ::String) at ./sysimg.jl:29
 [10] top-level scope at none:2
 [11] eval at ./boot.jl:319 [inlined]
 [12] eval(::Expr) at ./client.jl:389
 [13] top-level scope at ./none:3
in expression starting at moduleB.jl:5
in expression starting at moduleA.jl:4
ERROR: Failed to precompile moduleA [top-level] to moduleA.ji.
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] macro expansion at ./logging.jl:313 [inlined]
 [3] compilecache(::Base.PkgId, ::String) at ./loading.jl:1187
 [4] macro expansion at ./logging.jl:311 [inlined]
 [5] _require(::Base.PkgId) at ./loading.jl:944
 [6] require(::Base.PkgId) at ./loading.jl:855
 [7] macro expansion at ./logging.jl:311 [inlined]
 [8] require(::Module, ::Symbol) at ./loading.jl:837

```

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [November 2, 2018, 8:51am UTC](https://discourse.julialang.org/t/how-to-let-two-modules-use-part-of-each-other/17075/2 "2018-11-02T08:51:37Z")

</div>

I suggest you do this instead:

```julia
module ModuleB
export StructB

mutable struct StructB
	i::Int
end
end

module ModuleA
export StructA, func
using ..ModuleB: StructB

mutable struct StructA
	i::Int
	SB::StructB
end

function func(A::StructA)
    A.i += 1
end
end

using .ModuleA, .ModuleB
func(StructA(1, StructB(1)))

```

or if `func` needs to belong to `ModuleB` then this

```julia
module ModuleB
export StructB, func

mutable struct StructB
	i::Int
end
function func end
end

module ModuleA
export StructA
using ..ModuleB: StructB
import ..ModuleB: func

mutable struct StructA
	i::Int
	SB::StructB
end

function func(A::StructA)
    A.i += 1
end
end

using .ModuleA, .ModuleB
func(StructA(1, StructB(1)))

```

If you really, really need to do it in your original way then this works:

```julia
module ModuleB
export StructB

mutable struct StructB
	i::Int
end
end

module ModuleA
export StructA
using ..ModuleB: StructB

mutable struct StructA
	i::Int
	SB::StructB
end
end

ModuleB.@eval begin
    using ..ModuleA: StructA
    export func
    function func(A::StructA)
	A.i += 1
    end
    nothing
end

using .ModuleA, .ModuleB
func(StructA(1, StructB(1)))

```

but that code sure is smelly.

PS: convention has it to name modules camel case, but with upper first letter. Have a read through [Style Guide · The Julia Language](https://docs.julialang.org/en/v1/manual/style-guide/).

---

<div class="post-metadata">

### Author: ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)
#### Post date: [November 2, 2018, 4:57pm UTC](https://discourse.julialang.org/t/how-to-let-two-modules-use-part-of-each-other/17075/3 "2018-11-02T16:57:25Z")

</div>

One other technique I’ve seen for this issue: if type `A` needs to depend on type `B` that isn’t defined yet, then instead you can make `A` depend on a type parameter `T`, and later use `A{B}` once `B` is defined.
