From 0777b0e08c9fe67408934558d5fa611b67699fc2 Mon Sep 17 00:00:00 2001 From: Rafael Fourquet Date: Thu, 4 Jun 2020 14:38:40 +0200 Subject: [PATCH] fix ImmutableDict(pairs...) constructor 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). --- base/dict.jl | 2 ++ test/dict.jl | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/base/dict.jl b/base/dict.jl index 1760e62338cf8..1f42ab8c82f85 100644 --- a/base/dict.jl +++ b/base/dict.jl @@ -739,6 +739,8 @@ Create a new entry in the `ImmutableDict` for a `key => value` pair ImmutableDict ImmutableDict(KV::Pair{K,V}) where {K,V} = ImmutableDict{K,V}(KV[1], KV[2]) ImmutableDict(t::ImmutableDict{K,V}, KV::Pair) where {K,V} = ImmutableDict{K,V}(t, KV[1], KV[2]) +ImmutableDict(t::ImmutableDict{K,V}, KV::Pair, rest::Pair...) where {K,V} = + ImmutableDict(ImmutableDict(t, KV), rest...) ImmutableDict(KV::Pair, rest::Pair...) = ImmutableDict(ImmutableDict(KV), rest...) function in(key_value::Pair, dict::ImmutableDict, valcmp=(==)) diff --git a/test/dict.jl b/test/dict.jl index 73e8e5bad6e2e..c281c0cda9424 100644 --- a/test/dict.jl +++ b/test/dict.jl @@ -723,7 +723,9 @@ import Base.ImmutableDict d5 = ImmutableDict(v...) @test d5 == d2 @test reverse(collect(d5)) == v - @test ImmutableDict(:a => 1, :a => 2)[:a] == 2 + d6 = ImmutableDict(:a => 1, :b => 3, :a => 2) + @test d6[:a] == 2 + @test d6[:b] == 3 @test !haskey(ImmutableDict(-0.0=>1), 0.0) end