# Trying to require Modules in a circular way, program just crashes

**URL:** <https://discourse.julialang.org/t/trying-to-require-modules-in-a-circular-way-program-just-crashes/15596>\
**Category:** General Usage\
**Created:** [September 28, 2018, 8:44am UTC](https://discourse.julialang.org/t/trying-to-require-modules-in-a-circular-way-program-just-crashes/15596 "2018-09-28T08:44:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 28, 2018, 8:44am UTC](https://discourse.julialang.org/t/trying-to-require-modules-in-a-circular-way-program-just-crashes/15596/1 "2018-09-28T08:44:50Z")

</div>

I got 2 files, and in each file I include` the other file so it goes like

 ![](https://global.discourse-cdn.com/julialang/original/3X/3/d/3daf334812cedbe6ea3e01bd5895115edbef8836.png)  
When I run this code it just errors hard:  
 ![](https://global.discourse-cdn.com/julialang/original/3X/a/e/aea0389f58f841db601068b9c86ec0c5abe95140.png)  
Is there any way around it?

---

<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:** [September 28, 2018, 9:11am UTC](https://discourse.julialang.org/t/trying-to-require-modules-in-a-circular-way-program-just-crashes/15596/2 "2018-09-28T09:11:07Z")

</div>

There is probably a better way to do what you want, you just have to explain it.

Also, please quote your code:

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)\
**Post date:** [September 28, 2018, 9:47am UTC](https://discourse.julialang.org/t/trying-to-require-modules-in-a-circular-way-program-just-crashes/15596/3 "2018-09-28T09:47:32Z")

</div>

Are you trying to emulate some OO-style by using modules? Anyway, here’s how you can do it if you want to define the actual types in different modules.

In file `TwoFiles.jl` I have

```julia
abstract type AbstractClient end
abstract type AbstractManager end

include("client.jl")
include("manager.jl")

println("Yay! 🍕 for everyone!")

```

In file `client.jl` I have

```julia
module JClient
    import ..AbstractClient, ..AbstractManager
    export self

    struct self{TM<:AbstractManager}<:AbstractClient
        token::String
        ws::TM
    end
end

```

and in file `manager.jl` I have

```julia
module WSManager
    import ..AbstractClient, ..AbstractManager
    export Manager

    struct Manager{C<:AbstractClient} <: AbstractManager
        client::C
    end
end

```

this gives

 ![pizza](https://global.discourse-cdn.com/julialang/original/3X/0/2/027bae69f66b9db73ee3d7876391fbbf1b454520.png)

So using abstract types can get around the fact that the two modules don’t know about each others types directly.

Edit: I should say it doesn’t do exactly what you had, obviously those fields can now also take other AbstractClient types and AbstractManager types, but…

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 28, 2018, 9:48am UTC](https://discourse.julialang.org/t/trying-to-require-modules-in-a-circular-way-program-just-crashes/15596/4 "2018-09-28T09:48:38Z")

</div>

Oh wow thank you very much

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 28, 2018, 9:59am UTC](https://discourse.julialang.org/t/trying-to-require-modules-in-a-circular-way-program-just-crashes/15596/5 "2018-09-28T09:59:18Z")

</div>

Another question, can I access the fields when I pass them to the struct?

---

<div class="post-metadata">

**Author:** ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)\
**Post date:** [September 28, 2018, 10:59am UTC](https://discourse.julialang.org/t/trying-to-require-modules-in-a-circular-way-program-just-crashes/15596/6 "2018-09-28T10:59:04Z")

</div>

I don’t understand what you mean. What did you try?

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [September 28, 2018, 3:58pm UTC](https://discourse.julialang.org/t/trying-to-require-modules-in-a-circular-way-program-just-crashes/15596/7 "2018-09-28T15:58:11Z")

</div>

I think OP wanted to afterwards create instances with circular (i.e. mutual) references, such that `some_client.ws.client === some_client`. Such a thing is afaik currently impossible, because julia currently does not support forward definitions of types.

Apart from the problem that, without having one of them mutable, you would not be able to construct such instances (and if you cheat and construct recursive immutable structs by pointer manipulation then you are likely to cause a crash).

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 28, 2018, 5:05pm UTC](https://discourse.julialang.org/t/trying-to-require-modules-in-a-circular-way-program-just-crashes/15596/8 "2018-09-28T17:05:40Z")

</div>

Creating two immutable objects that refer to each other is a bit of a chicken and egg problem.
