# Mutually recursive types, recommended style?

**URL:** <https://discourse.julialang.org/t/mutually-recursive-types-recommended-style/128228>\
**Category:** General Usage\
**Tags:** struct\
**Created:** [April 19, 2025, 5:55pm UTC](https://discourse.julialang.org/t/mutually-recursive-types-recommended-style/128228 "2025-04-19T17:55:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DrEntropy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drentropy/32/216394_2.png) [@DrEntropy](https://discourse.julialang.org/u/DrEntropy)\
**Post date:** [April 19, 2025, 5:55pm UTC](https://discourse.julialang.org/t/mutually-recursive-types-recommended-style/128228/1 "2025-04-19T17:55:15Z")

</div>

When faced with a situation where you may need mutually recursive types like this:

```julia
mutable struct MyAgent <: AbstractAgent
    id::Int
    friendB :: AbstractAgent # will always be AgentB type though
    #other fields
end

mutable struct AgentB <: AbstractAgent
    id::Int
    friendA :: AgentA 
    # etc
end

```

Is the internal constructor idiom still the recommended approach? (as described here: [Mutually recursive type - General Usage - Julia Programming Language](https://discourse.julialang.org/t/mutually-recursive-type/25536) ) or would it be better to use use integers and look up the object from a collection when needed? (ECS-like style). Better means “more idiomatic” and “more performant”.

---

<div class="post-metadata">

**Author:** ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)\
**Post date:** [April 19, 2025, 7:38pm UTC](https://discourse.julialang.org/t/mutually-recursive-types-recommended-style/128228/2 "2025-04-19T19:38:24Z")

</div>

(Minor comment, I know I’m not answering your question: MyAgent is an abstract container type, you may want to convert it to a parametric type for performance reasons)

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [April 20, 2025, 12:41am UTC](https://discourse.julialang.org/t/mutually-recursive-types-recommended-style/128228/3 "2025-04-20T00:41:39Z")

</div>

Neither is more idiomatic than the other, mutually recursive or even just recursive types are actually very rare in practice. If you use inlineable `isbits` element types for those collections, you could cut down on allocations at the cost of targeted manual memory management e.g. integers specifying indices of a collection doesn’t inherently keep it alive or the specified elements valid.
