# Retrieve the type of \`AbstractDict\` (without parameters) from a concrete dictionary type

**URL:** <https://discourse.julialang.org/t/retrieve-the-type-of-abstractdict-without-parameters-from-a-concrete-dictionary-type/67567>\
**Category:** General Usage\
**Created:** [September 2, 2021, 11:35am UTC](https://discourse.julialang.org/t/retrieve-the-type-of-abstractdict-without-parameters-from-a-concrete-dictionary-type/67567 "2021-09-02T11:35:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [September 2, 2021, 11:35am UTC](https://discourse.julialang.org/t/retrieve-the-type-of-abstractdict-without-parameters-from-a-concrete-dictionary-type/67567/1 "2021-09-02T11:35:51Z")

</div>

I want a function that takes an `AbstractDict` (it might be either `Dict`, an `OrderedDict`, etc.), and returns a dictionary of the same type, except for its parameters (the types of keys and values may differ). I fancied that this might be the way (toy example):

```julia
function dictoflengths(d::D{<:Any, <:AbstractVector}) where D
    y = length.(values(d))
    D(zip(keys(d), y))
end

```

But that’s not right, since when I define such function I get:

```julia
ERROR: TypeError: in Type{...} expression, expected UnionAll, got a value of type TypeVar

```

How could I do what I want? Thanks!

---

<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:** [September 2, 2021, 11:38am UTC](https://discourse.julialang.org/t/retrieve-the-type-of-abstractdict-without-parameters-from-a-concrete-dictionary-type/67567/2 "2021-09-02T11:38:20Z")

</div>

```julia

julia> function dictoflengths(d::D) where D<:AbstractDict{<:Any, <:AbstractVector}
           y = length.(values(d))
           D(zip(keys(d), y))
       end

```

I guess you can’t split how many type parameters from the type (`AbstractDict`). Although I think semantically, the syntax `where D<:AbstractDict` could have worked.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [September 2, 2021, 12:28pm UTC](https://discourse.julialang.org/t/retrieve-the-type-of-abstractdict-without-parameters-from-a-concrete-dictionary-type/67567/3 "2021-09-02T12:28:40Z")

</div>

I missed to add `<:AbstractDict` after `where D` in the function signature, but that doesn’t work yet. The function you suggest can be defined, but it doesn’t do what I want:

```julia-repl
julia> function dictoflengths(d::D) where D<:AbstractDict{<:Any, <:AbstractVector}
           y = length.(values(d))
           D(zip(keys(d), y))
       end
dictoflengths (generic function with 1 method)

julia> dictoflengths(OrderedDict(:a=>[1,2,3],:b=>[1,5]))
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Vector{Int64}

```

With that signature, `D` is a concrete type (`OrderedDict{Symbol,Vector{Float64}}`), whereas I wanted `OrderedDict` (a `UnionAll`).

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [September 2, 2021, 12:53pm UTC](https://discourse.julialang.org/t/retrieve-the-type-of-abstractdict-without-parameters-from-a-concrete-dictionary-type/67567/4 "2021-09-02T12:53:54Z")

</div>

I think I got it, after a bit of introspecting into data types:

```julia
get_rawtype(D::DataType) = getproperty(parentmodule(D), nameof(D))

function dictoflengths(d::D) where D<:AbstractDict{<:Any, <:AbstractVector}
    T = get_rawtype(D)
    y = length.(values(d))
    T(zip(keys(d), y))
end

```

Any caveat about this going into the module that defines the `UnionAll` type to fetch it?

---

<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:** [September 2, 2021, 1:07pm UTC](https://discourse.julialang.org/t/retrieve-the-type-of-abstractdict-without-parameters-from-a-concrete-dictionary-type/67567/5 "2021-09-02T13:07:43Z")

</div>

if you’re not using the type parameter of D, I guess you don’t need the `{}`
