# Why are self-referential struct parameters impossible?

**URL:** https://discourse.julialang.org/t/why-are-self-referential-struct-parameters-impossible/110982
**Category:** Internals & Design
**Tags:** type, parametric-types, recursion, parameters
**Created:** [February 29, 2024, 11:18pm UTC](https://discourse.julialang.org/t/why-are-self-referential-struct-parameters-impossible/110982 "2024-02-29T23:18:12Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 29, 2024, 11:35pm UTC](https://discourse.julialang.org/t/why-are-self-referential-struct-parameters-impossible/110982/2 "2024-02-29T23:35:30Z")

</div>

> [@mnemnion](#):
>
> ```julia
> struct Tree{V<:Any,T<:Union{Tree,Nothing}}
> value::V
> left::T
> right::T 
> end 
> 
> ```

You should be wary of this design anyway, because then your tree would consist of nodes of different types and your tree-traversal code will require dynamic dispatch. Probably it’s better to put the `Union` in the field type directly, as in [this definition of red–black trees in DataStructures.jl](https://github.com/JuliaCollections/DataStructures.jl/blob/5451407b8c7e66a2e572d2f50c0f75e675016d35/src/red_black_tree.jl#L5-L15), so that all nodes in a given tree have the same type.

(Worse, with your sort of definition, the root node would encode the entire structure of the tree into its type. Not only would every node have a different type, but every tree structure would have a different type — you’d be trying to get the compiler to re-specialize your code for every distinct tree structure.)

---

_[View the full topic](https://discourse.julialang.org/t/why-are-self-referential-struct-parameters-impossible/110982)._
