-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Implement setindex for ImmutableDict #59880
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
base: master
Are you sure you want to change the base?
Conversation
This adds a method of `Base.setindex` for `Base.ImmutableDict`, as suggested in JuliaObjects/Accessors.jl#216. For a key that is already in the `ImmutableDict`, rather than add it again and shadow it, as described in the `ImmutableDict` doc string, I opted to exclude the original key and then add in the new one.
This is a bit misleading since it isn't changing the original dictionary but creating a new one. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes the complexity of ImmutableDict worse than Vector{Pair}, which is never useful
Did you perhaps misread this as
Whenever I make a PR for Julia, it never takes me long to get in over my head... So, take this all with a grain of salt. But the more I look at When I saw I think my main question is whether The most apparent ways it violates the expectations of a dictionary is the behavior of julia> d = Base.ImmutableDict(:a => 1, :b => 2, :a => 3)
Base.ImmutableDict{Symbol, Int64} with 3 entries:
:a => 3
:b => 2
:a => 1
julia> keys(d)
KeySet for a Base.ImmutableDict{Symbol, Int64} with 3 entries. Keys:
:a
:b
:a
julia> values(d)
ValueIterator for a Base.ImmutableDict{Symbol, Int64} with 3 entries. Values:
3
2
1
julia> collect(d)
3-element Vector{Pair{Symbol, Int64}}:
:a => 3
:b => 2
:a => 1 I worry that users will iterate over an As another example, consider this: julia> d1 = Base.ImmutableDict(:a => 1, :a => 2)
Base.ImmutableDict{Symbol, Int64} with 2 entries:
:a => 2
:a => 1
julia> d2 = Base.ImmutableDict(:a => 2, :a => 1)
Base.ImmutableDict{Symbol, Int64} with 2 entries:
:a => 1
:a => 2
julia> d1[:a]
2
julia> d2[:a]
1
julia> d1 == d2
true Is this a bug? No matter whether Getting back to my PR. If function setindex(d::ImmutableDict, value, key)
ImmutableDict(d, key => value)
end seems appropriate, and I can change this PR to do that. But if it is meant to be an immutable dictionary, then my original implementation seems better, despite the higher complexity, because it actually behaves like a dictionary. |
It could have been an AssociationList (taking the name from some lisps), to emphasize that implementation detail. But lisps seem to commonly emphasize implementation details rather than the abstract concepts when naming things. Elsewhere it is more common to refer to this as a generalization of a dict, e.g. https://pypi.org/project/multidict/ and https://en.cppreference.com/w/cpp/container/unordered_multimap/find.html and others in https://en.wikipedia.org/wiki/Multimap |
The |
This adds a method of
Base.setindex
forBase.ImmutableDict
, as suggested in JuliaObjects/Accessors.jl#216.For a key that is already in the
ImmutableDict
, rather than add it again and shadow it, as described in theImmutableDict
doc string, I opted to exclude the original key and then add in the new one.