Why can't I create an ImmutableDict from a vector of pairs (like for a normal cidt)

Minimal example:

pairs = [:A => 1, :B =>2]
d1 = Dict(pairs) # works
d2 = Base.ImmutableDict(pairs) # errors
d3 = Base.ImmutableDict(pairs...) # works

It seems strange that d2 fails? Is there a reason, or a way to avoid splatting (I’ve been told to avoid this due to performance issues)?

Maybe open an issue? ImmutableDict has mostly been used as an internal type and I guess nobody needed that function in base

1 Like

Looks like the method for Dict(::Vector{Pair}) comes from here in the source. That function looks fairly generic, so I suspect it would work for most iterable types that generate Pairs. the ImmutableDict type is defined here, where there are definitely fewer methods defined.

It’s interesting that ImmutableDict is described in the docs (here) but isn’t actually exported (would be here). Not being exported often indicates that it’s an “internal” utility and not considered part of the API, so could be subject to breaking changes… but then why advertise it in the docs?

Edit: looks like this was previously noticed and commented on in this Issue.

1 Like

Interesting to know. I am implementing metadata for an (internal) structure within Catalyst.jl. Since it is based on ModelingToolkit.jl, which uses ImmutableDict for its metadata, I decided to also go with it.