Skip to content

Commit e073818

Browse files
committed
Implement setindex for Base.ImmutableDict
The design of Base.ImmutableDict allows the same key to be added again, which shadows the previously added value. Unfortunately, it still prints out all of the entries, including repeated keys. I thought this might be confusing, so I implemented it to remove the value if the key exists and then add the new value. Closes #216
1 parent 4e3d9e5 commit e073818

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/setindex.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ Base.@propagate_inbounds function setindex(d0::AbstractDict, v, k)
2424
return d
2525
end
2626

27+
function setindex(d0::Base.ImmutableDict, v, k)
28+
d = Base.ImmutableDict(
29+
(
30+
key => value
31+
for (key, value) in d0 if key != k
32+
)...
33+
)
34+
Base.ImmutableDict(d, k => v)
35+
end
36+
2737
# may be added to Base in https://github.com/JuliaLang/julia/pull/46453
2838
@inline function setindex(t::Tuple, v, inds::AbstractVector{<:Integer})
2939
ntuple(length(t)) do i

0 commit comments

Comments
 (0)