# Narrowing types on a Dict

**URL:** <https://discourse.julialang.org/t/narrowing-types-on-a-dict/1608>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 20, 2017, 10:02am UTC](https://discourse.julialang.org/t/narrowing-types-on-a-dict/1608 "2017-01-20T10:02:29Z")\
**Posts on this page:** 4\
**Page:** 1

<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:** [January 20, 2017, 10:02am UTC](https://discourse.julialang.org/t/narrowing-types-on-a-dict/1608/1 "2017-01-20T10:02:29Z")

</div>

Suppose I am accumulating key-value pairs of a previously unknown type in a `Dict{Any,Any}` within a function. But before I return the result, I would like to narrow the type parameters as much as possible.

Is there some compact syntax for that? I came up with

```julia
Dict(Base.Generator(identity, dict))

```

but it looks like a kludge.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 20, 2017, 10:15am UTC](https://discourse.julialang.org/t/narrowing-types-on-a-dict/1608/2 "2017-01-20T10:15:51Z")

</div>

You can do the following for example:

```julia
julia> d = Dict()
Dict{Any,Any} with 0 entries

julia> d[3] = "hello"; d[4] = "goodbye"
"goodbye"

julia> d
Dict{Any,Any} with 2 entries:
  4 => "goodbye"
  3 => "hello"

julia> Dict(Pair(kv) for kv in d) # kv are key-value pairs
Dict{Int64,String} with 2 entries:
  4 => "goodbye"
  3 => "hello"

```

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [January 20, 2017, 10:37am UTC](https://discourse.julialang.org/t/narrowing-types-on-a-dict/1608/3 "2017-01-20T10:37:00Z")

</div>

Nice. With the definitions of your example, `Dict(kv for kv in d)` seems to work too.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 20, 2017, 11:01am UTC](https://discourse.julialang.org/t/narrowing-types-on-a-dict/1608/4 "2017-01-20T11:01:10Z")

</div>

Not quite sure why that works but `Dict(d...)` doesn’t.
