# Self-referential struct and invalid redefinition error

**URL:** <https://discourse.julialang.org/t/self-referential-struct-and-invalid-redefinition-error/21340>\
**Category:** General Usage\
**Created:** [March 1, 2019, 12:15pm UTC](https://discourse.julialang.org/t/self-referential-struct-and-invalid-redefinition-error/21340 "2019-03-01T12:15:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![HiroIshida](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hiroishida/32/7424_2.png) [@HiroIshida](https://discourse.julialang.org/u/HiroIshida)\
**Post date:** [March 1, 2019, 12:15pm UTC](https://discourse.julialang.org/t/self-referential-struct-and-invalid-redefinition-error/21340/1 "2019-03-01T12:15:05Z")

</div>

Let’s consider a file named `test.jl` that includes the following self-referential struct:

```julia
mutable struct Node
    ID::Int
    child::Union{Node, Nothing}
end

```

Somehow, even if I don’t change the file at all, hitting twice the command `include("./test.jl")` in the REPL arises an error: `ERROR: LOADError: invalid redefinition of constant Node`. So, on each such an occasion, I have to quit and enter the REPL again, which is time-consuming especially when file-size is larger.

Does anyone know how to efficiently avoid this problem? Or, would you point out if I do something wrong?

I’m using Julia version 1.0.1 on Ubuntu 16.04.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 1, 2019, 12:17pm UTC](https://discourse.julialang.org/t/self-referential-struct-and-invalid-redefinition-error/21340/2 "2019-03-01T12:17:13Z")

</div>

[https://github.com/JuliaLang/julia/issues/21816](https://github.com/JuliaLang/julia/issues/21816)

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 1, 2019, 12:20pm UTC](https://discourse.julialang.org/t/self-referential-struct-and-invalid-redefinition-error/21340/3 "2019-03-01T12:20:36Z")

</div>

I would suggest using Revise.jl to avoid the need to include your file multiple times altogether.

---

<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:** [March 1, 2019, 3:43pm UTC](https://discourse.julialang.org/t/self-referential-struct-and-invalid-redefinition-error/21340/4 "2019-03-01T15:43:03Z")

</div>

A simple fix is to wrap it in a module:

```julia
julia> module _nodemod
       mutable struct Node
           ID::Int
           child::Union{Node, Nothing}
       end
       end

```

However, you then need to qualify access by `_nodemod.Node`.
