Possible to set the type of Dict entries for specific keys?

Hi all,

I would like to use a Dict (or a similar structure) with values that can have different, but concrete types for given keys. For example:

mydict = Dict(:a=> 2, :b=>[1.0, 3.0], :c=> "hello type")

In general, this means typeof(mydict) is Dict{Symbol, Any}, so entries of mydict are type Any. However, I want to use mydict in a way, that the type of key :a is always Int, the type of key :b is always Vector{Float64} and so on.

Is there a way to set a specific type of the entries for the keys :a, :b and :c? Or could one implement a custom “Dict” that can do so?

I hope I made my question clear. I’m happy for answers!

Best

Dict doesn’t support this, but this sounds like exactly what mutable struct already does. Can you do something like this?

mutable struct MyType
  a::Int 
  b::Vector{Float64}
end

yes, that makes sense. What I need is a bit more complex however. If b inside of MyType is in turn something like a mutable struct, that would probably do it for me. Is this a good approach / style?

yeah sure, just like class, struct can and should be used when needed to have hierarchy.

Absolutely!

perfect, thanks both of you!