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

**URL:** <https://discourse.julialang.org/t/possible-to-set-the-type-of-dict-entries-for-specific-keys/36159>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 18, 2020, 8:00pm UTC](https://discourse.julialang.org/t/possible-to-set-the-type-of-dict-entries-for-specific-keys/36159 "2020-03-18T20:00:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlanghinrichs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlanghinrichs/32/50371_2.png) [@mlanghinrichs](https://discourse.julialang.org/u/mlanghinrichs)\
**Post date:** [March 18, 2020, 8:00pm UTC](https://discourse.julialang.org/t/possible-to-set-the-type-of-dict-entries-for-specific-keys/36159/1 "2020-03-18T20:00:38Z")

</div>

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:

```julia
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

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 18, 2020, 8:07pm UTC](https://discourse.julialang.org/t/possible-to-set-the-type-of-dict-entries-for-specific-keys/36159/2 "2020-03-18T20:07:46Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![mlanghinrichs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlanghinrichs/32/50371_2.png) [@mlanghinrichs](https://discourse.julialang.org/u/mlanghinrichs)\
**Post date:** [March 18, 2020, 8:15pm UTC](https://discourse.julialang.org/t/possible-to-set-the-type-of-dict-entries-for-specific-keys/36159/3 "2020-03-18T20:15:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 18, 2020, 8:32pm UTC](https://discourse.julialang.org/t/possible-to-set-the-type-of-dict-entries-for-specific-keys/36159/4 "2020-03-18T20:32:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 18, 2020, 8:40pm UTC](https://discourse.julialang.org/t/possible-to-set-the-type-of-dict-entries-for-specific-keys/36159/5 "2020-03-18T20:40:08Z")

</div>

> [@mlanghinrichs](#):
>
> 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?

Absolutely!

---

<div class="post-metadata">

**Author:** ![mlanghinrichs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlanghinrichs/32/50371_2.png) [@mlanghinrichs](https://discourse.julialang.org/u/mlanghinrichs)\
**Post date:** [March 18, 2020, 8:47pm UTC](https://discourse.julialang.org/t/possible-to-set-the-type-of-dict-entries-for-specific-keys/36159/6 "2020-03-18T20:47:51Z")

</div>

perfect, thanks both of you!
