Leveraging the abstract type seems to be the intended use for situations with circularity.
abstract type AbstractChild end
mutable struct Parent <: AbstractChild
name::String
children::Vector{AbstractChild}
Parent(name) = new(name, Vector{AbstractChild}())
end
mutable struct Child <: AbstractChild
name::String
parents::Tuple{AbstractChild, AbstractChild}
Child(name) = new(name)
end
link(parent1::Parent, parent2::Parent, child::Child) = (
push!(parent1.children, child);
push!(parent2.children, child);
child.parents = (parent1, parent2)
)
father = Parent("father")
mother = Parent("mother")
child = Child("son")
link(father, mother, child)
Running the code & inspecting the structures produces the following output (Juila 1.9.4):
julia> father
Parent("father", AbstractChild[Child("son", (Parent(#= circular reference @-4 =#), Parent("mother", AbstractChild[Child(#= circular reference @-4 =#)])))])
julia> mother
Parent("mother", AbstractChild[Child("son", (Parent("father", AbstractChild[Child(#= circular reference @-4 =#)]), Parent(#= circular reference @-4 =#)))])
julia> child
Child("son", (Parent("father", AbstractChild[Child(#= circular reference @-4 =#)]), Parent("mother", AbstractChild[Child(#= circular reference @-4 =#)])))
According to [link], #= circular reference @-4 =# is a good sign that we have setup the two-way reference properly.