Behavior of ImmutableDict

Can anyone explain why ImmutableDict works the way it does?

I can’t specify the type when declaring it:

# Produces MethodError.
weapons = ImmutableDict{String,Int32}("Light-Rifle" => 100, "Plasma-Pistol" => 50)

I can’t pass in an array of pairs:

weapon_pairs = Pair{String,Int32}[("Light-Rifle" => 100),("Plasma Pistol" => 50)]
# Produces MethodError.
weapons = ImmutableDict(weapon_pairs)

It’s considered an AbstractDict:

println(isa(weapons,AbstractDict))  # true

Why can’t I specify the KV types or pass in an array of pairs?

ImmutableDict has a limited and convoluted way to add elements:

julia> import Base: ImmutableDict

julia> weapons = ImmutableDict{String,Int32}("Light-Rifle", 100)
ImmutableDict{String, Int32} with 1 entry:
  "Light-Rifle" => 100

julia> weapons = ImmutableDict{String,Int32}(weapons, "Plasma Pistol", 50)
ImmutableDict{String, Int32} with 2 entries:
  "Plasma Pistol" => 50
  "Light-Rifle"   => 100

Probably there are very few users of this type (it isn’t even exported) that no one got around making its usage more comfortable

1 Like

The funny part is that it’s in the documentation, so I decided to try it out for myself.

When you say hasn’t been exported, you mean, I can’t use it naturally, I have to import from Base before using, correct?

I don’t think ImmutableDict is intended to be used outside of Base. Where did you find out about it?


(I use it outside of Base :eyes: well, I reimplemented it to not do type piracy)