Julia dict and haskey

Hi all,

I am reading the documentation of Dict and haskey(), but I cannot figure this out.

I want to create a dictionary, where I want to be able to feed in an Integer, and then get the corresponding Float64.

Thus, I define:

s = Dict{Int, Float64}
const y = 3

haskey(s, y)

This gives me an error: MethodError: no method matching haskey(::Type{Dict{Int64, Int64}}, ::Int64)

Why is that? In my view, it should give me false, as the dictionary is empty and does not contain the integer 3 to look up.

Thanks!

Your s is only the type, not a Dict itself. Try:

s = Dict{Int, Float64}()
const y = 3

haskey(s, y)

Note the additional () after Dict{Int, Float64}.

1 Like

I see, of course, that makes so much sense. I must have overlooked that detail, thanks for pointing it out

1 Like