Self-referential struct and invalid redefinition error

#1

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

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.

#2
2 Likes
#3

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

2 Likes
#4

A simple fix is to wrap it in a module:

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

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

1 Like