# Create a Dict from eltype

**URL:** <https://discourse.julialang.org/t/create-a-dict-from-eltype/1742>\
**Category:** General Usage\
**Created:** [January 28, 2017, 4:17pm UTC](https://discourse.julialang.org/t/create-a-dict-from-eltype/1742 "2017-01-28T16:17:41Z")\
**Posts on this page:** 5\
**Page:** 1

<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 28, 2017, 4:17pm UTC](https://discourse.julialang.org/t/create-a-dict-from-eltype/1742/1 "2017-01-28T16:17:41Z")

</div>

Is there a reason why dict cannot be constructed from its element type `eltype(d)=Pair{K,V}` but requires to pass key and value types? The constructor for a dict from its element type is `similar(d, eltype(d))`.  
For a vector, the round trip holds: `T=eltype(Vector{T}())`.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 28, 2017, 4:50pm UTC](https://discourse.julialang.org/t/create-a-dict-from-eltype/1742/2 "2017-01-28T16:50:22Z")

</div>

You mean that you want to do `Dict{eltype(d)}()`? The reason that this doesn’t work is that the thing in `{...}` must be the parameters of the type, and `Dict` is parameterized by `{K,V}`, the key `K` and value `V` types, not by a `Pair{K,V}`. You can use `similar` if you want to pass a `Pair` of types, e.g. `similar(d, eltype(d))` and `similar(d, Pair{Int,Int})` work.

---

<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 28, 2017, 5:12pm UTC](https://discourse.julialang.org/t/create-a-dict-from-eltype/1742/3 "2017-01-28T17:12:22Z")

</div>

I am trying to write the documentation for `similar(dict)` and that is the motivation to ask the question: `similar` seems to be the constructor of a dictionary from `Pair`.

Why can’t I construct any container from its element type? To me it makes sense to have `T=eltype(Dict{T}())` in the same way as we have `T=eltype(Vector{T}())`.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 28, 2017, 5:43pm UTC](https://discourse.julialang.org/t/create-a-dict-from-eltype/1742/4 "2017-01-28T17:43:59Z")

</div>

> [@mzaffalon](#):
>
> To me it makes sense to have `T=eltype(Dict{T}())`

In practice, you commonly want to write functions operating on `Dict` that make use of the key and value types individually, so it is much more convenient to parameterize `Dict{K,V}` by the key and value types individually than by `Pair{K,V}`. The current approach also allows you to write `Dict{K}` to describe a family of `Dict` types with the same key type `K` but different value types.

---

<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 28, 2017, 6:47pm UTC](https://discourse.julialang.org/t/create-a-dict-from-eltype/1742/5 "2017-01-28T18:47:31Z")

</div>

OK for the practical approach.
