# How to self reference a struct

**URL:** https://discourse.julialang.org/t/how-to-self-reference-a-struct/91795
**Category:** New to Julia
**Created:** [December 17, 2022, 9:44pm UTC](https://discourse.julialang.org/t/how-to-self-reference-a-struct/91795 "2022-12-17T21:44:11Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Ray\_Walker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ray_walker/32/45181_2.png) [@Ray\_Walker](https://discourse.julialang.org/u/Ray_Walker)
#### Post date: [December 17, 2022, 9:44pm UTC](https://discourse.julialang.org/t/how-to-self-reference-a-struct/91795/1 "2022-12-17T21:44:11Z")

</div>

How do you define an array in a struct of itself?

julia\> mutable struct node  
level::Int  
candidate::Int  
children::Array{node, 3}  
end  
ERROR: invalid redefinition of constant node  
Stacktrace:  
[1] top-level scope  
@ REPL[5]:1

---

<div class="post-metadata">

### Author: ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)
#### Post date: [December 17, 2022, 9:57pm UTC](https://discourse.julialang.org/t/how-to-self-reference-a-struct/91795/2 "2022-12-17T21:57:46Z")

</div>

Interesting, I guess the following would work:

```julia
abstract type AbstractNode end
struct Node{T<:AbstractNode}<:AbstractNode
    level::Int
    candidate::Int
    children::Array{T,3}
end

```

But I think you’d never be able to instantiate this since you can’t use leaf nodes, or they can’t be null. You probably want to use a union or something to make it practical.

EDIT: As stated below, an empty array is perfectly fine, so there’s not an issue with instantiation.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [December 17, 2022, 11:46pm UTC](https://discourse.julialang.org/t/how-to-self-reference-a-struct/91795/3 "2022-12-17T23:46:48Z")

</div>

> [@Ray\_Walker](#):
>
> ERROR: invalid redefinition of constant node

That should work, I’ve done such constructs a number of times - do you perhaps already have something called `node` in that session? That’s what the error suggests, at least. What do you get in a new session?

Also, please quote your code, to make it easier to help you.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

> [@jmair](#):
>
> But I think you’d never be able to instantiate this since you can’t use leaf nodes, or they can’t be null

You can use that - leaves just don’t have children, the array is empty. You can even have a `parent` field too - if the parent is equal to the object itself, you’re at the root. It’s also a bit more efficient to do it this way, since there’s no type instability involved anymore, unlike with the union approach.

---

<div class="post-metadata">

### Author: ![Ray\_Walker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ray_walker/32/45181_2.png) [@Ray\_Walker](https://discourse.julialang.org/u/Ray_Walker)
#### Post date: [December 19, 2022, 8:11pm UTC](https://discourse.julialang.org/t/how-to-self-reference-a-struct/91795/4 "2022-12-19T20:11:24Z")

</div>

I think that may have been the case. Thank you so much for your help. Closing.
