# Struct with array of pointers to other structs of the same type as field

**URL:** https://discourse.julialang.org/t/struct-with-array-of-pointers-to-other-structs-of-the-same-type-as-field/73248
**Category:** General Usage
**Tags:** struct
**Created:** [December 17, 2021, 8:16am UTC](https://discourse.julialang.org/t/struct-with-array-of-pointers-to-other-structs-of-the-same-type-as-field/73248 "2021-12-17T08:16:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![BelaMulder](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@BelaMulder](https://discourse.julialang.org/u/BelaMulder)
#### Post date: [December 17, 2021, 8:16am UTC](https://discourse.julialang.org/t/struct-with-array-of-pointers-to-other-structs-of-the-same-type-as-field/73248/1 "2021-12-17T08:16:20Z")

</div>

Newbie question: I would like to implement lattice structure as an array of structs named Node, where each Node contains as one of its field a list of its neighbouring Nodes in the lattice. My, possibly naive, first try

```julia
mutable struct Node
    value :: Int
    neighbors :: Array{Union{Node,Int},1}
end

```

where I use the Union type to duck the issue of self-reference works, but I wonder if there is a more elegant way, and if there are no performance penalties incurred by using the Union type in this way.

---

<div class="post-metadata">

### Author: ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)
#### Post date: [December 17, 2021, 5:49pm UTC](https://discourse.julialang.org/t/struct-with-array-of-pointers-to-other-structs-of-the-same-type-as-field/73248/2 "2021-12-17T17:49:36Z")

</div>

A struct can reference itself no problem:

```julia
mutable struct Node
    value :: Int
    neighbors :: Array{Node,1}
end

```

---

<div class="post-metadata">

### Author: ![BelaMulder](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@BelaMulder](https://discourse.julialang.org/u/BelaMulder)
#### Post date: [December 20, 2021, 7:33am UTC](https://discourse.julialang.org/t/struct-with-array-of-pointers-to-other-structs-of-the-same-type-as-field/73248/3 "2021-12-20T07:33:20Z")

</div>

Thanks, I was apparently misled by some earlier post that suggested otherwise.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [December 20, 2021, 1:48pm UTC](https://discourse.julialang.org/t/struct-with-array-of-pointers-to-other-structs-of-the-same-type-as-field/73248/4 "2021-12-20T13:48:29Z")

</div>

I think it was was not always possible, I remember having problems with self-referencing structures more than a single time in the last years. But it may be that the problem only arises if the structure is directly in a field, not wrapped inside another container.
