Type stability nested dictionary

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.

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.

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

1 Like

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)

I think that what you are looking for is:

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

T can be any type, but it is not the Any type. :wink:

1 Like