# How to do I define a struct A which refer a struct B which refer A?

**URL:** <https://discourse.julialang.org/t/how-to-do-i-define-a-struct-a-which-refer-a-struct-b-which-refer-a/65373>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 27, 2021, 1:48pm UTC](https://discourse.julialang.org/t/how-to-do-i-define-a-struct-a-which-refer-a-struct-b-which-refer-a/65373 "2021-07-27T13:48:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zwtechnb](https://avatars.discourse-cdn.com/v4/letter/z/65b543/32.png) [@zwtechnb](https://discourse.julialang.org/u/zwtechnb)\
**Post date:** [July 27, 2021, 1:48pm UTC](https://discourse.julialang.org/t/how-to-do-i-define-a-struct-a-which-refer-a-struct-b-which-refer-a/65373/1 "2021-07-27T13:48:17Z")

</div>

For example:

```julia
mutable struct A
a::Int
b::Ref{B}
end

mutable struct B
b::Int
a::Ref{A}
end

```

---

<div class="post-metadata">

**Author:** ![zwtechnb](https://avatars.discourse-cdn.com/v4/letter/z/65b543/32.png) [@zwtechnb](https://discourse.julialang.org/u/zwtechnb)\
**Post date:** [July 27, 2021, 2:16pm UTC](https://discourse.julialang.org/t/how-to-do-i-define-a-struct-a-which-refer-a-struct-b-which-refer-a/65373/2 "2021-07-27T14:16:00Z")

</div>

Finally, I got a solution by `Incomplete Initialization` @Julia Manual.  
Solution:

```julia
mutable struct A{T}
    a::Int
    b::Ref{T}
    A{T}(a::Int) where T = new{T}(a)
end

mutable struct B
    b::Int
    a::Ref{A{T}} where T
    B(b::Int)=new(b)
end

a =A{B}(1)
b = B(2)
a.b=Ref{B}()
a.b[]=b
a.b[].b == 2

b.a=Ref{A{B}}()
b.a[] = a
b.a[].a == 1

```
