# Are circular references problematic?

**URL:** <https://discourse.julialang.org/t/are-circular-references-problematic/93767>\
**Category:** General Usage\
**Created:** [January 30, 2023, 12:48pm UTC](https://discourse.julialang.org/t/are-circular-references-problematic/93767 "2023-01-30T12:48:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mariok90](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariok90/32/7076_2.png) [@mariok90](https://discourse.julialang.org/u/mariok90)\
**Post date:** [January 30, 2023, 12:48pm UTC](https://discourse.julialang.org/t/are-circular-references-problematic/93767/1 "2023-01-30T12:48:18Z")

</div>

I have this structure in my code:

```julia
abstract type SystemComponent end

mutable struct System
    components::Dict{String, <:SystemComponent}

    function System()
        new(Dict{String, Component}())
    end
end

mutable struct Component<: SystemComponent
    label::AbstractString
    parent::System
    components::Dict{String, <:SystemComponent}

    function Component(label::AbstractString, parent::System)
        self = new(label, parent, Dict{String, SystemComponent}())
        parent.components[label] = self
        return self
    end
end

sys = System()
comp1 = Component("comp1", sys)

```

When I print `sys` it get:  
`System(Dict{String, Component}("comp1" => Component("comp1", System(#= circular reference @-3 =#), Dict{String, SystemComponent}())))`

Is the `#= circular reference @-3 =#` something I should worry about? My quick research indicated that circular reference should be avioded. However, is that the case here? What would be an alternative approach?

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [January 30, 2023, 1:30pm UTC](https://discourse.julialang.org/t/are-circular-references-problematic/93767/2 "2023-01-30T13:30:52Z")

</div>

What you see in the output (`#= circular reference @-3 =#`) is not an issue itself. It is just a way to show instances with circular references.

If Julia did not care about circular references when showing instances, you would get stuck in a loop (show system → show component → show system → …). This is essentially the reason why circular references are a bad idea. When the code that your using does not account for circular references, you can get errors (see, e.g., [StackOverflow in hash for circular references · Issue #46725 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/46725)).

An alternative approach depends on the problem. Do you really need to have references in both directions (system → components, component → system)? For example, could you always start from `System` and go down to the components?

```julia
function some_function(system::System)
    for comp_name, comp in system.components
        # now "parent" == system
        do_something(system, comp)
    end
end

```

---

<div class="post-metadata">

**Author:** ![mariok90](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariok90/32/7076_2.png) [@mariok90](https://discourse.julialang.org/u/mariok90)\
**Post date:** [January 30, 2023, 2:05pm UTC](https://discourse.julialang.org/t/are-circular-references-problematic/93767/3 "2023-01-30T14:05:55Z")

</div>

Thank for the answer!

The idea is to inherit parameters from parent components. I have multiple layers of components and if a required parameter is not defined, the next place to look for is in the parent component. Addionally, I need to know which children each component has. So I kind a need both directions. However, I think it would be possible to handle these relations on the root level (=System) and avoid self references.

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [January 30, 2023, 2:36pm UTC](https://discourse.julialang.org/t/are-circular-references-problematic/93767/4 "2023-01-30T14:36:13Z")

</div>

Or use circular references and keep that in mind when writing code 🙂 It does not have to be wrong; sometimes it is necessary (think doubly linked lists).
