# Type stability nested dictionary

**URL:** <https://discourse.julialang.org/t/type-stability-nested-dictionary/38349>\
**Category:** General Usage\
**Tags:** dictionary, type-stability\
**Created:** [April 28, 2020, 5:52am UTC](https://discourse.julialang.org/t/type-stability-nested-dictionary/38349 "2020-04-28T05:52:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mopg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mopg/32/3368_2.png) [@mopg](https://discourse.julialang.org/u/mopg)\
**Post date:** [April 28, 2020, 5:52am UTC](https://discourse.julialang.org/t/type-stability-nested-dictionary/38349/1 "2020-04-28T05:52:05Z")

</div>

I am getting a nested input dictionary as input to my code, the structure of which I’d like to preserve. It is a nested dictionary where the “end state” is always an object of the same type (which I know). See the example below.

```julia
struct examplestruct
    x::Float64
    y::Int64
end

D = Dict(
    :child1=>Dict(
        :child11=>examplestruct(0.,1),
        :child12=>examplestruct(2.,5),
    ),
    :child2=>examplestruct(-1.,10)
)

struct parentstruct
    D::Dict{Symbol,Any}
end

```

This nested dictionary `D` here is saved in another `struct`, which introduces a type stability.

Does anyone have a good way around this type instability? The input will have to remain a nested structure, but I am allowed to change that structure inside my code (for computation). However, I would like to keep some form of the nested data structure, which is needed for output.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 28, 2020, 5:54am UTC](https://discourse.julialang.org/t/type-stability-nested-dictionary/38349/2 "2020-04-28T05:54:20Z")

</div>

Parametrize the type in `parentstruct`. See

[https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-abstract-container-1](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-abstract-container-1)

[https://docs.julialang.org/en/v1/manual/types/#Parametric-Types-1](https://docs.julialang.org/en/v1/manual/types/#Parametric-Types-1)

Incidentally, please follow the style conventions and capitalize type names.

[https://docs.julialang.org/en/v1/manual/style-guide/#Use-naming-conventions-consistent-with-Julia-base/-1](https://docs.julialang.org/en/v1/manual/style-guide/#Use-naming-conventions-consistent-with-Julia-base/-1)

---

<div class="post-metadata">

**Author:** ![mopg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mopg/32/3368_2.png) [@mopg](https://discourse.julialang.org/u/mopg)\
**Post date:** [April 28, 2020, 2:55pm UTC](https://discourse.julialang.org/t/type-stability-nested-dictionary/38349/3 "2020-04-28T14:55:19Z")

</div>

Thanks for the reply!

However, I don’t think that would completely solve the problem here, because the type of `D` is `Dict{Symbol,Any}`? (since the level of nesting is not consistent)

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [April 28, 2020, 3:27pm UTC](https://discourse.julialang.org/t/type-stability-nested-dictionary/38349/4 "2020-04-28T15:27:52Z")

</div>

I think that what you are looking for is:

```julia
struct ParentStruct{T}
    d::Dict{Symbol, T}
end

```

`T` can be any type, but it is not _the_ `Any` type. 😉
