# Create \`ImmutableDict\` with values of differenrt types

**URL:** https://discourse.julialang.org/t/create-immutabledict-with-values-of-differenrt-types/107831
**Category:** General Usage
**Created:** [December 19, 2023, 9:25pm UTC](https://discourse.julialang.org/t/create-immutabledict-with-values-of-differenrt-types/107831 "2023-12-19T21:25:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [December 19, 2023, 9:25pm UTC](https://discourse.julialang.org/t/create-immutabledict-with-values-of-differenrt-types/107831/1 "2023-12-19T21:25:50Z")

</div>

Simple MWE:

```julia
d1 = Dict(:a => 1.0, :b => "Hello world") # Works
d2 = Base.ImmutableDict(:a => 1.0, :b => "Hello world") # Fails

```

How do I create an `ImmutableDict` where keys can have arbitrary values?

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [December 19, 2023, 9:38pm UTC](https://discourse.julialang.org/t/create-immutabledict-with-values-of-differenrt-types/107831/2 "2023-12-19T21:38:09Z")

</div>

There might be some room for interface improvement here. But the following works:

```julia
d = Base.ImmutableDict(Base.ImmutableDict{Symbol, Any}(),
  :a => 1.0, :b => "Hello strange world")

```

This workaround (which necessitated looking at the code) is because the underlying linked list has to have the same key-value types, and is initialized by the first pair. The empty Dict in the example above fixes the value type to be Any.

A little less verbose:

```julia
d = Base.ImmutableDict(Pair{Symbol,Any}(:a, 1.0), :b => "Hi strange world")

```

Another version:

```julia
d = Base.ImmutableDict(Base.ImmutableDict{Symbol,Any}(:a, 1.0), :b => "Hi")

```

---

<div class="post-metadata">

### Author: ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)
#### Post date: [December 20, 2023, 10:49am UTC](https://discourse.julialang.org/t/create-immutabledict-with-values-of-differenrt-types/107831/3 "2023-12-20T10:49:20Z")

</div>

As Dan said, ImmutableDict is based on a linked list.

This is a very niche datastructure for very specialized use-cases. It is not an appropriate general purpose datastructure for an “immutable dictionary”.

If you need to ask, you should probably not use ImmutableDict. The entire thing should probably have been julia-internal only.

(ImmutableDict is good for cases where you must use an API that insists on an AbstractDict, and you have 0-5 key-value pairs, and you possibly get to share the tails of the linked list between multiple ImmutableDicts)

---

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [December 20, 2023, 11:02am UTC](https://discourse.julialang.org/t/create-immutabledict-with-values-of-differenrt-types/107831/4 "2023-12-20T11:02:22Z")

</div>

Got it, thanks for the input.

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. This is the reason for using an internal Julia type.
