3

how can is solve this problem?

mutable struct Parent
    name::String
    children::Vector{Child}

    function Parent(name)
        return new(name)

    end

end

mutable struct Child
    name::String
    parent::Parent

    function Child(name)
        return new(name)

    end

end

parent = Parent("father")
child = Child("son")

Produces an error

LoadError: UndefVarError: Child not defined

Is there some way to handle this case?

2
  • Mutually recursive types are still an [open issue] in the language. Commented Jun 3, 2019 at 8:03
  • (missing link) Commented Jun 3, 2019 at 8:14

3 Answers 3

8

As far as I know the only way to handle this currently is via a parametric type (I know it is not perfect). Here is an example which additionally restricts the parameter so that you get almost what you want:

abstract type AbstractChild end

mutable struct Parent{T<:AbstractChild}
    name::String
    children::Vector{T}
    function Parent{T}(name) where {T<:AbstractChild}
        return new{T}(name)
    end

end

mutable struct Child <: AbstractChild
    name::String
    parent::Parent

    function Child(name)
        return new(name)
    end
end

Parent(name) = Parent{Child}(name)

parent = Parent("father")
child = Child("son")
Sign up to request clarification or add additional context in comments.

1 Comment

Presumably you would also want to instantiate the child with the parent you just created: child = Child("son", parent) and then add the child to parent.children with push!(parent.children, child).
0

And just to add on to @Bogumił Kamiński's answer the abstract type AbstractChild end creates a node for julia to traverse from at runtime in the program.

1 Comment

This should be a comment to Bogumil's answer, instead of a seperate answer.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.