# 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:** 1
**Showing post:** 3

<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.

---

_[View the full topic](https://discourse.julialang.org/t/how-to-self-reference-a-struct/91795)._
