Skip to content

Commit 60bab00

Browse files
rfourquetKristofferC
authored andcommitted
fix ImmutableDict(pairs...) constructor (#36143)
It could only handle a couple of pairs, e.g. ImmutableDict(1=>1, 2=>2, 3=>3) would throw. The fix is implemented by adding the `ImmutableDict(t::ImmutableDict, pairs...)` constructor, which generalizes `ImmutableDict(t::ImmutableDict, pair)` (with some similarity to how `push!` accepts multiple items to be pushed). (cherry picked from commit 162cde1)
1 parent 5c236b4 commit 60bab00

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

base/dict.jl

+2
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,8 @@ Create a new entry in the `ImmutableDict` for a `key => value` pair
739739
ImmutableDict
740740
ImmutableDict(KV::Pair{K,V}) where {K,V} = ImmutableDict{K,V}(KV[1], KV[2])
741741
ImmutableDict(t::ImmutableDict{K,V}, KV::Pair) where {K,V} = ImmutableDict{K,V}(t, KV[1], KV[2])
742+
ImmutableDict(t::ImmutableDict{K,V}, KV::Pair, rest::Pair...) where {K,V} =
743+
ImmutableDict(ImmutableDict(t, KV), rest...)
742744
ImmutableDict(KV::Pair, rest::Pair...) = ImmutableDict(ImmutableDict(KV), rest...)
743745

744746
function in(key_value::Pair, dict::ImmutableDict, valcmp=(==))

test/dict.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,9 @@ import Base.ImmutableDict
723723
d5 = ImmutableDict(v...)
724724
@test d5 == d2
725725
@test reverse(collect(d5)) == v
726-
@test ImmutableDict(:a => 1, :a => 2)[:a] == 2
726+
d6 = ImmutableDict(:a => 1, :b => 3, :a => 2)
727+
@test d6[:a] == 2
728+
@test d6[:b] == 3
727729

728730
@test !haskey(ImmutableDict(-0.0=>1), 0.0)
729731
end

0 commit comments

Comments
 (0)