From fe593469a057bce9f3a905141dbfca360edbdac5 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Thu, 14 May 2020 19:34:39 -0700 Subject: [PATCH] Left fold ImmutableDict(args::Pair...) (#35871) Currently, the behavior of `Dict(pairs...)` and `Base.ImmutableDict(pairs...)` are different: julia> Dict(:a => 1, :a => 2)[:a] 2 julia> Base.ImmutableDict(:a => 1, :a => 2)[:a] 1 This PR fixes the latter to return 2. --- base/dict.jl | 2 +- test/dict.jl | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/base/dict.jl b/base/dict.jl index b3387d84c9ca2..109538e70cf4f 100644 --- a/base/dict.jl +++ b/base/dict.jl @@ -742,7 +742,7 @@ 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(KV::Pair, rest::Pair...) = ImmutableDict(ImmutableDict(rest...), KV) +ImmutableDict(KV::Pair, rest::Pair...) = ImmutableDict(ImmutableDict(KV), rest...) function in(key_value::Pair, dict::ImmutableDict, valcmp=(==)) key, value = key_value diff --git a/test/dict.jl b/test/dict.jl index 16a1b13f62e3b..73e8e5bad6e2e 100644 --- a/test/dict.jl +++ b/test/dict.jl @@ -722,7 +722,8 @@ import Base.ImmutableDict v = [k1 => v1, k2 => v2] d5 = ImmutableDict(v...) @test d5 == d2 - @test collect(d5) == v + @test reverse(collect(d5)) == v + @test ImmutableDict(:a => 1, :a => 2)[:a] == 2 @test !haskey(ImmutableDict(-0.0=>1), 0.0) end