# How to interactively refer to other modules' type?

**URL:** <https://discourse.julialang.org/t/how-to-interactively-refer-to-other-modules-type/47540>\
**Category:** New to Julia\
**Tags:** question, package\
**Created:** [September 30, 2020, 1:40pm UTC](https://discourse.julialang.org/t/how-to-interactively-refer-to-other-modules-type/47540 "2020-09-30T13:40:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [September 30, 2020, 1:40pm UTC](https://discourse.julialang.org/t/how-to-interactively-refer-to-other-modules-type/47540/1 "2020-09-30T13:40:05Z")

</div>

I’m confused with interactively referring types across modules.  
Here’s my example:

- `src/MyModuleA.jl`

```julia
module A

using Reexport

include("MyModuleB.jl")
include("MyModuleC.jl")

@reexport using .B
@reexport using .C

end

```

- `src/MyModuleB.jl`

```julia
module B

export MyStructB

mutable struct MyStructB
    b
end

end

```

- `src/MyModuleC.jl`

```julia
module C

export MyStructC

include("MyModuleB.jl")
using .B

mutable struct MyStructC
    myB::MyStructB
    MyStructC() = init!(new())
end

function init!(myC::MyStructC)
    myC.myB = MyStructB(1.0)
    return myC
end

end

```

- `test/MyTest.jl`

```julia
include("../src/MyModuleA.jl")
using .A

myC = MyStructC()
typeof(myC.myB) == MyStructB

```

- Result (after running `include("test/MyTest.jl")`

```julia
false # I expected `MyStructB`, but the type is `Main.A.C.B.MyStructB`

```

As above, I expected it should be `MyStructB` but it’s `Main.A.C.B.MyStructB`.

Anybody can help me?

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [September 30, 2020, 1:48pm UTC](https://discourse.julialang.org/t/how-to-interactively-refer-to-other-modules-type/47540/2 "2020-09-30T13:48:46Z")

</div>

After reading Reexport’s examples more carefully, I found out what should I do.  
I changed a little bit of codes like this and it solves the problem:

- `src/MyModuleA.jl`

```julia
module A

using Reexport

# include("MyModuleB.jl")
include("MyModuleC.jl")

# @reexport using .B
@reexport using .C

end

```

- `src/MyModuleB.jl`

```julia
module B

export MyStructB

mutable struct MyStructB
    b
end

end

```

- `src/MyModuleC.jl`

```julia
module C

export MyStructC

using Reexport

include("MyModuleB.jl")
@reexport using .B

mutable struct MyStructC
    myB::MyStructB
    MyStructC() = init!(new())
end

function init!(myC::MyStructC)
    myC.myB = MyStructB(1.0)
    return myC
end

end

```

- `test/MyTest.jl`

```julia
include("../src/MyModuleA.jl")
using .A

myC = MyStructC()
typeof(myC.myB) == MyStructB

```

- Result (after running `include("test/MyTest.jl")`

```julia
true

```

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [September 30, 2020, 2:00pm UTC](https://discourse.julialang.org/t/how-to-interactively-refer-to-other-modules-type/47540/3 "2020-09-30T14:00:01Z")

</div>

It is generally a bad idea to `include` the same file multiple times.

In `MyModuleC` you probably should use `using ../B`. Now you only have one version of `B` instead of two.

Often (I find) the cleaner alternative is to make `B` a package (even if it is small) and declare the dependency in `A`’s and `C`’s `Project.toml`, avoiding all `include` statements.

I have also found that having lots of sub-modules tends to be more trouble than it’s worth. But I don’t know anything about your project, so my experience may not apply here.
