# Type checking in Dictionaries

**URL:** https://discourse.julialang.org/t/type-checking-in-dictionaries/43156
**Category:** General Usage
**Created:** [July 16, 2020, 3:28am UTC](https://discourse.julialang.org/t/type-checking-in-dictionaries/43156 "2020-07-16T03:28:40Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)
#### Post date: [July 16, 2020, 3:28am UTC](https://discourse.julialang.org/t/type-checking-in-dictionaries/43156/1 "2020-07-16T03:28:40Z")

</div>

The Julia style guide says,

> For example, don’t declare an argument to be of type `Int` or [`Int32`](https://docs.julialang.org/en/v1/base/numbers/#Core.Int32) if it really could be any integer, expressed with the abstract type [`Integer`](https://docs.julialang.org/en/v1/base/numbers/#Core.Integer)  
> ([Numbers · The Julia Language](https://docs.julialang.org/en/v1/base/numbers/#Core.Integer))

However, this creates some issues when dealing with Dictionaries. As an example:

```julia
x = Dict(1=>[1,2,3])
display(x isa Dict{Integer,Vector{Integer}})
display(x isa Dict{Int64,Vector{Int64}})

```

The first option (using `Integer`) returns `false`. The second option (using `Int`) returns `true`. Any thoughts here?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [July 16, 2020, 3:47am UTC](https://discourse.julialang.org/t/type-checking-in-dictionaries/43156/2 "2020-07-16T03:47:07Z")

</div>

This is just that Julia’s types are invariant. You want `Dict{<:Integer, Vector{<:Integer}}`.

---

<div class="post-metadata">

### Author: ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)
#### Post date: [July 16, 2020, 4:09am UTC](https://discourse.julialang.org/t/type-checking-in-dictionaries/43156/3 "2020-07-16T04:09:18Z")

</div>

Adding the `<:` continues to return `false`

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [July 16, 2020, 4:24am UTC](https://discourse.julialang.org/t/type-checking-in-dictionaries/43156/4 "2020-07-16T04:24:52Z")

</div>

```julia
julia> x = Dict(1=>[1,2,3])
Dict{Int64,Array{Int64,1}} with 1 entry:
  1 => [1, 2, 3]

julia> x isa Dict{<:Integer, <:Vector{<:Integer}}
true

```

Here `Vector{<:Integer}` isa `UnionAll` type

```julia
julia> Vector{<:Integer}
Array{#s3,1} where #s3<:Integer

```

and `Vector{Int}` is a concrete type from the union.

---

<div class="post-metadata">

### Author: ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)
#### Post date: [July 16, 2020, 4:35am UTC](https://discourse.julialang.org/t/type-checking-in-dictionaries/43156/5 "2020-07-16T04:35:21Z")

</div>

> [@jishnub](#):
>
> `x isa Dict{<:Integer, <:Vector{<:Integer}}`

Ok. Adding the additional `<:` before `Vector` works. I guess for Dictionaries its just easier to disregard the Julia style guide and use `Int` instead of `Integer` with all those `<:`.

Thanks for the help.

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [July 16, 2020, 5:03am UTC](https://discourse.julialang.org/t/type-checking-in-dictionaries/43156/6 "2020-07-16T05:03:20Z")

</div>

Yes you should absolutely use concrete parametric types while creating objects, eg Dicts. The suggestion for abstract types is usually for function arguments

---

<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: [July 16, 2020, 8:36am UTC](https://discourse.julialang.org/t/type-checking-in-dictionaries/43156/7 "2020-07-16T08:36:43Z")

</div>

> [@hdavid16](#):
>
> I guess for Dictionaries its just easier to disregard the Julia style guide

No, the style guide is correct, what you are missing is the concept of _invariance_, see

[https://docs.julialang.org/en/v1/manual/types/#Parametric-Abstract-Types-1](https://docs.julialang.org/en/v1/manual/types/#Parametric-Abstract-Types-1)

The solution given by @jishnub is the correct way to dispatch on subtypes of a parametric type without narrowing down to a concrete one.

---

<div class="post-metadata">

### Author: ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)
#### Post date: [July 16, 2020, 10:43am UTC](https://discourse.julialang.org/t/type-checking-in-dictionaries/43156/8 "2020-07-16T10:43:31Z")

</div>

Good point. I see that there is a clear warning on this in the documentation right above the section on parametric abstract types. Thanks!

> Concrete `Point` types with different values of `T` are never subtypes of each other:

```julia
julia> Point{Float64} <: Point{Int64}
false

julia> Point{Float64} <: Point{Real}
false

```

Warning

This last point is _very_ important: even though `Float64 <: Real` we **DO NOT** have `Point{Float64} <: Point{Real}` .
