Dict key not found instead of type error?


julia> d = Dict(4 => "hola", 5 => "mundo")
Dict{Int64,String} with 2 entries:
  4 => "hola"
  5 => "mundo"

julia> d["wrong key type"]
ERROR: KeyError: key "wrong key type" not found
 [1] getindex(::Dict{Int64,String}, ::String) at ./dict.jl:474

I think this should throw a key type error, instead of a key not found error. I have had a few errors of these kind very hard to debug, because I was making the wrong assumption (that I had not added the key into the Dict), instead of focusing on a type mismatch.

A type error here would be more informative. In fact I would expect a “method does not exist” type of error. I don’t understand why getindex(::Dict{K,V}, ::T) does not require T == K (the definition should be getindex(::Dict{K,V},::K), shouldn’t it?).

Don’t you agree? I will raise an issue I am not missing nothing obvious.


No, dict keys are compared using isequal.

julia> Dict(1 => 2)[1.0]

There is no need to open an issue about this, see e.g. https://github.com/JuliaLang/julia/issues/15614