# How to define a nested type?

**URL:** <https://discourse.julialang.org/t/how-to-define-a-nested-type/92978>\
**Category:** General Usage\
**Created:** [January 15, 2023, 2:12am UTC](https://discourse.julialang.org/t/how-to-define-a-nested-type/92978 "2023-01-15T02:12:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wangyunyueyueniao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wangyunyueyueniao/32/44789_2.png) [@wangyunyueyueniao](https://discourse.julialang.org/u/wangyunyueyueniao)\
**Post date:** [January 15, 2023, 2:12am UTC](https://discourse.julialang.org/t/how-to-define-a-nested-type/92978/1 "2023-01-15T02:12:38Z")

</div>

For example: a nested dict with unknown depth.

```julia
Tree=Dict{Char,Union{Dict{Char,...},Int}}

```

I have tried this:

```julia
Tree=Dict{Char,Union{Tree,Int}}

```

But it gives “UndefVarError: Tree not defined”  
(I want to use this tree to develop a lexer)

---

<div class="post-metadata">

**Author:** ![torrance](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torrance/32/38990_2.png) [@torrance](https://discourse.julialang.org/u/torrance)\
**Post date:** [January 15, 2023, 2:45am UTC](https://discourse.julialang.org/t/how-to-define-a-nested-type/92978/2 "2023-01-15T02:45:27Z")

</div>

I had the exact same problem recently, and the issue here is that whilst Julia _does_ support recursive type declarations, this doesn’t work with type aliases (which is what you’re doing).

In the case of a type alias (e.g. `const MyTypeName = SomeOtherType`), Julia sees these as a regular assignment and so won’t allow you to assign something on the RHS to the LHS before the RHS value has first been declared.

A regular recursive type declaration however will work, so something like eg:

```julia
struct Tree
    child::Dict{Char, Union{Int, Tree}}
end

```

is fine and dandy. And I’m sure you can modify this kind of structure into something that works for you.

(I get that it’s a little less elegant than a straight dictionary, but you can add some methods onto `Tree` to pass on down into the dictionary and have it behave as you want).
