# Multiple nested module inclusion

**URL:** <https://discourse.julialang.org/t/multiple-nested-module-inclusion/62089>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 30, 2021, 1:09pm UTC](https://discourse.julialang.org/t/multiple-nested-module-inclusion/62089 "2021-05-30T13:09:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kalcool](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@kalcool](https://discourse.julialang.org/u/kalcool)\
**Post date:** [May 30, 2021, 1:09pm UTC](https://discourse.julialang.org/t/multiple-nested-module-inclusion/62089/1 "2021-05-30T13:09:10Z")

</div>

Hi there,

I am trying to build linked structures, which refer to each other, or to nothing/null, as you would do with pointers in C. In order to do this I am using abstract types and parametric structs in order to break the dependency recursion (as seen on Stack Overflow).  
At the same time I am trying to use modules to logically group these data structures with their respective functions. I have put the abstract types in their own module and now I am running into problems. see below for a minimum working example.

**Questions:**

- Is there a more idiomatic way to implement pointers than Union{Type1,Nothing}?
- How can I do “using abstract\_stuff” in multiple places and make julia understand that I am referring to the same abstract types?

Cheers,  
K

```julia
#abstractstuff.jl
module abstract_stuff
    export abstract_husband, abstract_wife
    abstract type abstract_husband end
    abstract type abstract_wife end
end

#REPL
module A
export husband
    include("abstractstuff.jl")
    using .abstract_stuff
    mutable struct husband{W<:abstract_wife}<:abstract_husband
        name::String
        my_wife::Union{W,Nothing}
    end
end
using .A

include("abstractstuff.jl")
using .abstract_stuff

mutable struct wife{H <: abstract_husband}<:abstract_wife
    name::String
    my_husband::Union{H,Nothing}
end

h=husband{wife}("bob",wife{husband}("cindy",nothing))
h.my_wife.my_husband = h

```

Result:

```julia
h=husband{wife}("bob",wife{husband}("cindy",nothing))
ERROR: TypeError: in husband, in W, expected W<:Main.A.abstract_stuff.abstract_wife, got Type{wife}
Stacktrace:
 [1] top-level scope
   @ REPL[6]:1

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 30, 2021, 3:21pm UTC](https://discourse.julialang.org/t/multiple-nested-module-inclusion/62089/2 "2021-05-30T15:21:01Z")

</div>

Generally this kind of linked list may not be the most efficient for Julia. It is hard to suggest something very specific without more context, but I would use something like this:

```julia
using ArgCheck

mutable struct Male
    name::String
    wife_index::Int # 0: single
end

mutable struct Female
    name::String
    husband_index::Int # 0: single
end

men = [Male(name, 0) for name in men_names]
women = [Female(name, 0) for name in women_names]

function marry!(men, women, husband_index, wife_index)
    male = men[husband_index]
    female = woman[wife_index]
    @argcheck male.wife_index == 0
    @argcheck female.husband_index == 0
    male.wife_index = wife_index
    female.husband_index = husband_index
end

```

---

<div class="post-metadata">

**Author:** ![kalcool](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@kalcool](https://discourse.julialang.org/u/kalcool)\
**Post date:** [May 30, 2021, 7:07pm UTC](https://discourse.julialang.org/t/multiple-nested-module-inclusion/62089/3 "2021-05-30T19:07:01Z")

</div>

I was hoping for automatic garbage collection. Using array indices as a hand rolled addressing scheme seems to force me to do my own memory management, doesn’t it?

---

<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:** [May 30, 2021, 7:17pm UTC](https://discourse.julialang.org/t/multiple-nested-module-inclusion/62089/4 "2021-05-30T19:17:13Z")

</div>

This would be a simpler scheme…

```julia
julia> mutable struct Person
         name::String
         married_to::Union{Person,Nothing}
       end

julia> bob = Person("Bob",nothing)
Person("Bob", nothing)

julia> cindy = Person("Cindy",nothing)
Person("Cindy", nothing)

julia> function marry(a,b)
         a.married_to = b
         b.married_to = a
       end
marry (generic function with 1 method)

julia> marry(bob,cindy)
Person("Bob", Person("Cindy", Person(#= circular reference @-2 =#)))

julia> bob
Person("Bob", Person("Cindy", Person(#= circular reference @-2 =#)))

julia> cindy
Person("Cindy", Person("Bob", Person(#= circular reference @-2 =#)))

```

But it is possible that this circular reference brings issues which I do not immediately see. Do they?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 30, 2021, 10:41pm UTC](https://discourse.julialang.org/t/multiple-nested-module-inclusion/62089/5 "2021-05-30T22:41:33Z")

</div>

> [@kalcool](#):
>
> Is there a more idiomatic way to implement pointers than Union{Type1,Nothing}?

No. It is the more idiomatic solution for maybe-null pointers. If it was a tree, for example, you could also just use a `Vector` of `nodes` instead (with the empty `Vector` playing the role of `nothing`). The alternative for your case is your type itself having some object that represents the null value. You can create a `struct NoWife <: abstract_wife; end` and use the only object of this type (i.e., `NoWife()`) as sentinel for when there is no wife but then I do not see a great advantage over `Union{T, Nothing}`.

> [@kalcool](#):
>
> How can I do “using abstract\_stuff” in multiple places and make julia understand that I am referring to the same abstract types?

The problem you are having comes from the fact that, in Julia, you should never be `require`ing the same code at two or more places. You have some alternatives:

1. Create a package for `abstract_stuff`, `Pkg.develop` it, and then just call `using PackageName` without any `require`.
2. Hack `LOAD_PATH` to include the folder where `abstractstuff.jl` is inside then, just call `using ModuleName` without any require.
3. In your case, in which you are using the REPL, you should probably not be `include`ing the `abstractstuff.jl` inside the REPL anyway. You should access it from the module `A`, like `A.abstract_stuff.abstract_wife`., without needing to re-include anything.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 31, 2021, 6:35am UTC](https://discourse.julialang.org/t/multiple-nested-module-inclusion/62089/6 "2021-05-31T06:35:03Z")

</div>

Yes, if you want GC you can stick to your original scheme.
