# How to initialize a \`Dict\` with \`#undef\` content?

**URL:** <https://discourse.julialang.org/t/how-to-initialize-a-dict-with-undef-content/125154>\
**Category:** General Usage\
**Tags:** dictionary, dict\
**Created:** [January 24, 2025, 8:13am UTC](https://discourse.julialang.org/t/how-to-initialize-a-dict-with-undef-content/125154 "2025-01-24T08:13:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)\
**Post date:** [January 24, 2025, 8:13am UTC](https://discourse.julialang.org/t/how-to-initialize-a-dict-with-undef-content/125154/1 "2025-01-24T08:13:09Z")

</div>

I want to initialize a `Dict` comprised of some `#undef` values. How can I do this?  
I tried `Dict{String, Float64}(undef, 3)` but it throws the following error:

```julia
ERROR: MethodError: no method matching Dict{String, Float64}(::UndefInitializer, ::Int64)

```

I also tried `Dict{String, Float64}(undef=>undef)` but it threw the following error:

```julia
ERROR: MethodError: Cannot `convert` an object of type UndefInitializer to an object of type String
The function `convert` exists, but no method is defined for this combination of argument types.

```

Is it possible to initialize such a container?

---

<div class="post-metadata">

**Author:** ![karei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karei/32/214809_2.png) [@karei](https://discourse.julialang.org/u/karei)\
**Post date:** [January 24, 2025, 8:27am UTC](https://discourse.julialang.org/t/how-to-initialize-a-dict-with-undef-content/125154/2 "2025-01-24T08:27:23Z")

</div>

There is no way to directly define a `Dict{String, Float64}` that contains a `#undef` value. But you can create an empty Dict by using `Dict{String, Float64}()`.

Or you can create an undefined vector and string first, and then make them a Dict:

```julia
a=3 # or missing, nothing
b=string()
c=Dict{String, Float64}(b=>a)

```

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [January 24, 2025, 8:54am UTC](https://discourse.julialang.org/t/how-to-initialize-a-dict-with-undef-content/125154/3 "2025-01-24T08:54:03Z")

</div>

It makes sense for arrays, but what would that do for dictionaries?

What do you want to use it for? Do you want to pre-allocate many slots in the dictionary so that you can incrementally add many elements to it with minimum allocations (see `sizehint!`)?

---

<div class="post-metadata">

**Author:** ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)\
**Post date:** [January 24, 2025, 9:05am UTC](https://discourse.julialang.org/t/how-to-initialize-a-dict-with-undef-content/125154/4 "2025-01-24T09:05:25Z")

</div>

No, I want it to create an initialized dictionary that doesn’t contain certain values so the user won’t be able to interpret the created Dict. Like, _“oh, this doesn’t contain any valuable information”_.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [January 24, 2025, 12:07pm UTC](https://discourse.julialang.org/t/how-to-initialize-a-dict-with-undef-content/125154/5 "2025-01-24T12:07:37Z")

</div>

As far as I know `#undef` only exists in the context of references to mutable objects, e.g.

```julia
julia> mutable struct Foo end

julia> Ref{Foo}()
Base.RefValue{Foo}(#undef)

julia> Array{Foo}(undef, 1, 2)
1×2 Matrix{Foo}:
 #undef #undef

```

I.e. there’s no such thing as a `#undef` Float64 value.

Although not really what you asked for, LazilyInitializedFields.jl may be of interest in this context.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [January 25, 2025, 6:54am UTC](https://discourse.julialang.org/t/how-to-initialize-a-dict-with-undef-content/125154/6 "2025-01-25T06:54:04Z")

</div>

I’d say that canonically a Dict “marks” absent values by not having the corresponding key. So imo there is no need to have another special marker for “absent” values.  
Alternatively Julia offers `missing` which embodies exactly the absence of information. So you could use that to marke the absent values.

---

<div class="post-metadata">

**Author:** ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)\
**Post date:** [January 26, 2025, 1:10pm UTC](https://discourse.julialang.org/t/how-to-initialize-a-dict-with-undef-content/125154/7 "2025-01-26T13:10:28Z")

</div>

> not really what you asked for, [LazilyInitializedFields.jl](https://juliaregistries.github.io/General/packages/redirect_to_repo/LazilyInitializedFields) may be of interest in this context.

This can enormously help me in my structure designation! Thank you so much for mentioning it!
