diff --git a/src/Bijections.jl b/src/Bijections.jl index 020bae5..1281613 100644 --- a/src/Bijections.jl +++ b/src/Bijections.jl @@ -170,6 +170,17 @@ function dict_inverse(d::D) where {D<:AbstractDict} return inverse_dict_type(D)(reverse.(collect(d))) end +function dict_inverse(d::Base.ImmutableDict{K,V}) where {K,V} + # ImmutableDict does not have a ImmutableDict{K,V}(pairs...) constructor + # The version of the constructor with type parameters was overlooked in + # https://github.com/JuliaLang/julia/issues/35863 + d_inv = Base.ImmutableDict{V,K}() + for (k, v) in reverse.(collect(d)) + d_inv = Base.ImmutableDict{V,K}(d_inv, k, v) + end + return d_inv +end + """ inv(b::Bijection) diff --git a/test/runtests.jl b/test/runtests.jl index 495a02a..24450d3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -88,6 +88,13 @@ end @test length(b) == 2 @test b["one"] == 1 @test b["two"] == 2 + + # test construction from ImmutableDict + d = Base.ImmutableDict(1 => "one", 2 => "two") + b = Bijection(d) + @test length(b) == 2 + @test b[1] == "one" + @test b[2] == "two" end # Test inv function