# Wrong dictionary key type does not give type error

**URL:** https://discourse.julialang.org/t/wrong-dictionary-key-type-does-not-give-type-error/27515
**Category:** General Usage
**Created:** [August 13, 2019, 10:03pm UTC](https://discourse.julialang.org/t/wrong-dictionary-key-type-does-not-give-type-error/27515 "2019-08-13T22:03:57Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [August 13, 2019, 10:03pm UTC](https://discourse.julialang.org/t/wrong-dictionary-key-type-does-not-give-type-error/27515/1 "2019-08-13T22:03:57Z")

</div>

Shouldn’t the example give a type error instead of key not found error ??

```julia
julia> d=Dict{Tuple{Int,Int}, Int}()
Dict{Tuple{Int64,Int64},Int64} with 0 entries

julia> d[(1,1)] = 5
5

julia> d[(1,1)]
5

julia> d[1]
ERROR: KeyError: key 1 not found
Stacktrace:
 [1] getindex(::Dict{Tuple{Int64,Int64},Int64}, ::Int64) at .\dict.jl:478
 [2] top-level scope at none:0

julia> 

```

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [August 14, 2019, 1:38am UTC](https://discourse.julialang.org/t/wrong-dictionary-key-type-does-not-give-type-error/27515/2 "2019-08-14T01:38:57Z")

</div>

I doubt the `getindex` method for a dictionary is specialized on the key type. If you do `d[1] = 5` I bet you get a `TypeError`.

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [August 14, 2019, 5:00am UTC](https://discourse.julialang.org/t/wrong-dictionary-key-type-does-not-give-type-error/27515/3 "2019-08-14T05:00:12Z")

</div>

> [@kevbonham](#):
>
> If you do `d[1] = 5` I bet you get a `TypeError` .

you are correct sir ! 🙂

```julia
julia> d[1] = 5
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Tuple{Int64,Int64}
Closest candidates are:
  convert(::Type{T<:Tuple{Any,Vararg{Any,N} where N}}, ::T<:Tuple{Any,Vararg{Any,N} where N}) where T<:Tuple{Any,Vararg{Any,N} where N} at essentials.jl:274
  convert(::Type{T<:Tuple{Any,Vararg{Any,N} where N}}, ::Tuple{Any,Vararg{Any,N} where N}) where T<:Tuple{Any,Vararg{Any,N} where N} at essentials.jl:275
  convert(::Type{T<:Tuple}, ::CartesianIndex) where T<:Tuple at multidimensional.jl:130
  ...
Stacktrace:
 [1] setindex!(::Dict{Tuple{Int64,Int64},Int64}, ::Int64, ::Int64) at ./dict.jl:373
 [2] top-level scope at none:0

```

> [@kevbonham](#):
>
> I doubt the `getindex` method for a dictionary is specialized on the key type.

That certainly appears to be the case, and you seem to expect that. Why wouldn’t it be ?

---

<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: [August 14, 2019, 6:33am UTC](https://discourse.julialang.org/t/wrong-dictionary-key-type-does-not-give-type-error/27515/4 "2019-08-14T06:33:20Z")

</div>

> [@purplishrock](#):
>
> Shouldn’t the example give a type error instead of key not found error ??

I am not sure about this:

```julia
help?> TypeError
search: TypeError

  TypeError(func::Symbol, context::AbstractString, expected::Type, got)

  A type assertion failure, or calling an intrinsic function with an incorrect argument type.

```

I see neither here.

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [August 14, 2019, 11:12am UTC](https://discourse.julialang.org/t/wrong-dictionary-key-type-does-not-give-type-error/27515/5 "2019-08-14T11:12:25Z")

</div>

> [@Tamas\_Papp](#):
>
> I see neither here.

Good point - I was definitely conflating this specific `MethodError` with a `TypeError` - this inability to convert the type is the one I was expecting.

> [@purplishrock](#):
>
> That certainly appears to be the case, and you seem to expect that. Why wouldn’t it be ?

I’m just trying to imagine what the method signature would have to be in order to have it specialize in the type. Sure it could be done, but it seems more complicated than it’s worth for something that’s going to end at an error in either case. Then again, I’m no expert, so maybe it’s easier than I imagine (but I still don’t really see the benefit).

---

<div class="post-metadata">

### Author: ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)
#### Post date: [August 14, 2019, 12:35pm UTC](https://discourse.julialang.org/t/wrong-dictionary-key-type-does-not-give-type-error/27515/6 "2019-08-14T12:35:36Z")

</div>

```julia
julia> struct Five end;
julia> Base.isequal(::Five, ::Five)=true;
julia> Base.isequal(::Five, x)= isequal(x, 5);
julia> Base.isequal(x, ::Five)= isequal(x, 5);
julia> Base.hash(::Five, h::UInt)=Base.hash(5, h)

julia> d=Dict(1=>2, 5=>3);
julia> d[Five()]
3
julia> d[Five()]=1
ERROR: MethodError: Cannot `convert` an object of type Five to an object of type Int64

```
