Dear Julia colleagues,
The 'Dict' container in the standard library has the following undocumented
bad behavior: if the key type is mutable, and the user mutates a key that
is already in the dictionary, then the associated (key,value) entry becomes
a zombie: it is no longer accessible either under the old or the new key,
but it shows up in an iteration over the dictionary. See the example below.
This behavior is completely predictable to someone who knows how 'Dict' is
implemented, but it may be surprising to the new user.
I am in the process of implementing a balanced-tree-based 'SortedDict'
container for the data-structures library, and my container has even worse
behavior in that scenario: changing a key in the dictionary may break the
entire indexing structure.
I could avoid this problem, as could the library 'Dict', by deep-copying
the keys that are placed into the container.
The question for all of you: Is this behavior acceptable for Dict? for my
SortedDict? Or should one or both of them switch to 'deepcopy'?
-- Steve
julia> a = [1,2,3];
julia> b = [4,5,7];
julia> DD = [a=>25, b=>29]
Dict{Array{Int64,1},Int64} with 2 entries:
[4,5,7] => 29
[1,2,3] => 25
julia> DD[[1,2,3]]
25
julia> a[3]=9
9
julia> DD[[1,2,3]]
ERROR: key not found: [1,2,3]
in at dict.jl:615
julia> DD[[1,2,9]]
ERROR: key not found: [1,2,9]
in at dict.jl:615
julia> DD[[4,5,7]]
29
julia> DD
Dict{Array{Int64,1},Int64} with 2 entries:
[4,5,7] => 29
[1,2,9] => 25
julia> DD[a]
ERROR: key not found: [1,2,9]
in at dict.jl:615
julia>