-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert key to Dict key type before hashing #4166
Conversation
The try/catch definitely smells odd here. |
I agree that the try/catch is awkward, but I don't know a way around it. In That lossy integer conversions don't throw is a bit of a problem for Dicts even without this PR: julia> a = Dict{Int16, Any}()
Dict{Int16,Any}()
julia> a[65537] = 2
2
julia> a[int16(1)]
2 |
Some other options:
|
My preference would be to not call |
We rely pretty heavily on the convert call. Personally, I think that throwing errors on integer conversion is the right approach – the fact that we don't has slightly bothered me for a while now. But it would be a very big change. |
Why shouldn't we just throw errors when the |
Throwing on a |
Maybe we could rethrow anything that's not an |
To me this is a slightly clearer example of the root problem: julia> d = Dict{Float64,Any}()
Dict{Float64,Any}()
julia> d[1] = "foo"
"foo"
julia> d
[1.0=>"foo"]
julia> d[1]
ERROR: key not found: 1
in getindex at dict.jl:486 This patch feels really wrong. I think we need a different solution, but there's definitely a problem here. |
Jeff and I decided that the right thing to do here is to allow |
The great thing about this is it makes Dicts generically do the right thing consistent with however convert and isequal work. For example if we change isequal to be true for equal integers of different types, everything would go through. |
For numeric key types, at least, it seems like the right least-surprise thing to do would be to allow both |
After fc05d74:
This is because
setindex!()
callsconvert()
on the key butgetindex()
does not. This PR callsconvert()
inht_keyindex()
, which fixes this. I wrapped this intry
/catch
so thatget()
/getkey()
won't throw whenconvert(::KeyType, ::DictKeyType)
fails or doesn't exist.