# Unending error message upon including script1 in script2 AND vice versa at the same time

**URL:** https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589
**Category:** General Usage
**Tags:** error
**Created:** [January 25, 2018, 4:40am UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589 "2018-01-25T04:40:36Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)
#### Post date: [January 25, 2018, 4:40am UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/1 "2018-01-25T04:40:36Z")

</div>

I included script1.jl in script2.jl and script2.jl in script1.jl because both contained structs required in both the files. I got an unending “LoadError: LoadError: … : LoadError: StackOverlowError”.  
This makes sense but I think there should be a better way to handle this I had to force quit the REPL(Ctrl ^ Z).

---

<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: [January 25, 2018, 4:57am UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/2 "2018-01-25T04:57:04Z")

</div>

Probably the same as [https://github.com/JuliaLang/julia/issues/24592](https://github.com/JuliaLang/julia/issues/24592), although there it ends after 80000 lines of error.

---

<div class="post-metadata">

### Author: ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)
#### Post date: [January 25, 2018, 9:48am UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/3 "2018-01-25T09:48:30Z")

</div>

I think the interdependent objects from the files can be pooler and then logically formed into a hierarchy by the compiler. I mean just throwing a single error message is enough since the problems can be solved by distributing the objects in the two files differently. But this would be cool!

---

<div class="post-metadata">

### Author: ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)
#### Post date: [January 25, 2018, 5:54pm UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/4 "2018-01-25T17:54:26Z")

</div>

Can someone tell me how to solve this? I have to define the interdependent mutable structs in order plus now I have to keep them distributed. But I still can’t skip the looping error since they end up making a loop or being defined in the wrong order…

---

<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: [January 25, 2018, 5:58pm UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/5 "2018-01-25T17:58:06Z")

</div>

So you’re question is about how to have mutually circular type definitions, and not about the error (as your first post suggest)? If so, have a look at [handle mutually-circular type declarations · Issue #269 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/269).

---

<div class="post-metadata">

### Author: ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)
#### Post date: [January 25, 2018, 6:09pm UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/6 "2018-01-25T18:09:21Z")

</div>

The solution given there, which is to use a begin block doesn’t work either. And no I wanted to report the error problem.

---

<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: [January 25, 2018, 6:10pm UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/7 "2018-01-25T18:10:44Z")

</div>

If two things keep calling each other forever, it’s a stack overflow… now sure what else could possibly happen here.

---

<div class="post-metadata">

### Author: ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)
#### Post date: [January 25, 2018, 7:13pm UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/8 "2018-01-25T19:13:05Z")

</div>

Can we reduce the very big error message?  
And since begin block isn’t working, it boils down to not specifying the actual precise data type that leads to the circular nature. Can we do something about that? Modelling real objects with optimisation is difficult without it. Even creating algorithmic objects. Unless you specify an additional abstract type to encapsulate the other. 🤔

---

<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: [January 25, 2018, 7:27pm UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/9 "2018-01-25T19:27:51Z")

</div>

Why don’t you add your example to the issue I linked above, or if you think it’s different open a new one?

Regarding the types, you can use parameters as a work-around:

```julia
struct Foo{T}
   bar::T 
end
struct Bar{T}
   foo::T 
end

```

x-ref: [https://github.com/JuliaLang/julia/issues/269#issuecomment-68421745](https://github.com/JuliaLang/julia/issues/269#issuecomment-68421745)

---

<div class="post-metadata">

### Author: ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)
#### Post date: [January 25, 2018, 7:40pm UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/10 "2018-01-25T19:40:27Z")

</div>

Okay. Thanks

---

<div class="post-metadata">

### Author: ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)
#### Post date: [January 26, 2018, 7:57am UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/11 "2018-01-26T07:57:03Z")

</div>

I did that by creating an abstract type. Eg:

```julia
abstract type SomeType end
mutable struct One <: SomeType
two::SomeType
x::Bool
end

mutable struct Three
ones::AbstractArray{One}
end

mutable struct Two <: SomeType
threes::AbstractArray{Three}
end

```

---

<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: [January 26, 2018, 8:17am UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/12 "2018-01-26T08:17:27Z")

</div>

Note that `One.two` has non-concrete type (see performance-tips in docs). Thus, if you use it somewhere performance critical better do:

```julia
mutable struct One{T<:SomeType} <: SomeType
two::T
x::Bool
end

```

---

<div class="post-metadata">

### Author: ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)
#### Post date: [January 26, 2018, 8:26am UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/13 "2018-01-26T08:26:52Z")

</div>

It is throws an error. 😕

```julia
LoadError: LoadError: invalid redefinition of constant One

```

---

<div class="post-metadata">

### Author: ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)
#### Post date: [January 26, 2018, 8:31am UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/14 "2018-01-26T08:31:17Z")

</div>

I did this instead, seems to work.  
Will this still be good for performance?

```julia
mutable struct One <:SomeType
    two::T where T<:SomeType
    something::Float16
end

```

---

<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: [January 26, 2018, 8:38am UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/15 "2018-01-26T08:38:17Z")

</div>

> [@abshej](#):
>
> It is throws an error. 😕

Start a new REPL.

The other alternative you show below is not concretely typed, so it won’t help.

Reading through the manual, at least the types part and the performance FAQ, may be helpful at this stage.

---

<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: [January 26, 2018, 8:41am UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/16 "2018-01-26T08:41:58Z")

</div>

> Reading through the manual…

I can recommend that too!

You can check whether Julia can infer types like so (slightly simplified your example):

```julia
julia> abstract type SomeType end

julia> struct One <:SomeType
           two::T where T<:SomeType
           something::Float16
       end

julia> struct Two <: SomeType end

julia> f(t) = t.two                                                                                                                                                      
f (generic function with 1 method)                                                                                                                                       

julia> @code_warntype f(One(Two(), 5))                                                                                                                                   
Variables:                                                                                                                                                               
  t::One                                                                                                                                                                 

Body:
  begin                                                                                                                                                                  
      # meta: location sysimg.jl getproperty 8                                                                                                                           
      SSAValue(1) = (Base.getfield)(t::One, :two)::SomeType                                                                                                              
      # meta: pop location
      return SSAValue(1)                                                                                                                                                 
  end::SomeType                                                                                                                                                          

```

This shows that Julia doesn’t know the concrete return type, only that it must be `<:SomeType`.

---

<div class="post-metadata">

### Author: ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)
#### Post date: [January 26, 2018, 9:07am UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/17 "2018-01-26T09:07:57Z")

</div>

Thank you @Tamas_Papp and @mauro3  
This has been very helpful indeed.

---

<div class="post-metadata">

### Author: ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)
#### Post date: [January 26, 2018, 9:12am UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/18 "2018-01-26T09:12:14Z")

</div>

Also should I use

```julia
mutable struct A{T<:Parent} <:Parent

```

for all structs under Parent or just for the structs which directly use another struct from the same Parent?

---

<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: [January 26, 2018, 11:33am UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/19 "2018-01-26T11:33:32Z")

</div>

Please do read the manual thorougly and get back here if you have questions. At this point answering this would just repeat sections from the manual.

---

<div class="post-metadata">

### Author: ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)
#### Post date: [January 26, 2018, 11:37am UTC](https://discourse.julialang.org/t/unending-error-message-upon-including-script1-in-script2-and-vice-versa-at-the-same-time/8589/20 "2018-01-26T11:37:06Z")

</div>

Thank you.
