# What is the type of #undef

**URL:** <https://discourse.julialang.org/t/what-is-the-type-of-undef/32411>\
**Category:** General Usage\
**Created:** [December 17, 2019, 11:42pm UTC](https://discourse.julialang.org/t/what-is-the-type-of-undef/32411 "2019-12-17T23:42:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [December 17, 2019, 11:42pm UTC](https://discourse.julialang.org/t/what-is-the-type-of-undef/32411/1 "2019-12-17T23:42:13Z")

</div>

```julia
struct MMX
           x
           MMX() = new()
       end

t = MMX()
> MMX(#undef)
typeof(t.x)

```

Basically I am asking what is the type of field x in this case?

---

<div class="post-metadata">

**Author:** ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Post date:** [December 18, 2019, 12:01am UTC](https://discourse.julialang.org/t/what-is-the-type-of-undef/32411/2 "2019-12-18T00:01:10Z")

</div>

Weird but interesting question…

```julia
julia> typeof(undef)

UndefInitializer

julia> typeof(UndefInitializer)
DataType

dump(undef)
UndefInitializer array initializer with undefined values

show(undef)
array initializer with undefined values

```

---

<div class="post-metadata">

**Author:** ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Post date:** [December 18, 2019, 12:04am UTC](https://discourse.julialang.org/t/what-is-the-type-of-undef/32411/3 "2019-12-18T00:04:16Z")

</div>

[https://github.com/JuliaLang/julia/blob/0b80774b9f0a517f12714e30808915e99fd916ff/base/boot.jl#L402](https://github.com/JuliaLang/julia/blob/0b80774b9f0a517f12714e30808915e99fd916ff/base/boot.jl#L402)

Apparently it’s a quintessential empty struct!

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [December 18, 2019, 12:04am UTC](https://discourse.julialang.org/t/what-is-the-type-of-undef/32411/4 "2019-12-18T00:04:29Z")

</div>

```nohighlight
julia> struct OO
       x
       end

julia> OO(undef)
OO(array initializer with undefined values)

julia> t = ans
OO(array initializer with undefined values)

julia> t.x
array initializer with undefined values

```

I don’t think what you say is quite right because in the former case

```nohighlight
struct MMX
           x
           MMX() = new()
       end

t = MMX()
julia> t.x
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] getproperty(::Any, ::Symbol) at ./Base.jl:20

```

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [December 18, 2019, 12:06am UTC](https://discourse.julialang.org/t/what-is-the-type-of-undef/32411/5 "2019-12-18T00:06:28Z")

</div>

I’d say it’s `Union{}`.

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [December 18, 2019, 12:09am UTC](https://discourse.julialang.org/t/what-is-the-type-of-undef/32411/6 "2019-12-18T00:09:09Z")

</div>

If so, how can I instantiate Union{} or I can ask this question another way, How can I create and undefined reference object if something like that exists.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [December 18, 2019, 1:02am UTC](https://discourse.julialang.org/t/what-is-the-type-of-undef/32411/7 "2019-12-18T01:02:39Z")

</div>

You can’t create an undefined reference object, but that is exactly the point. It stands for a stack-allocated object, that has not been defined yet. A classic use case is for vectors, e.g. `Vector{SomeType}(undef, n)`, whose values will be overwritten anyways, so there’s no need to fill them with a value. `undef` is just a singleton object signifying, that a vector or a mutable struct does not need to be initialized. If `SomeType` is heap-allocated, on the other hand, it will just be some random value that has been in this location in memory, so this shouldn’t usually be relied upon. The reason this works for your type `MMX` is that `x` is of type `Any`, which means it is boxed, so it can be `#undef`.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [December 18, 2019, 1:50am UTC](https://discourse.julialang.org/t/what-is-the-type-of-undef/32411/8 "2019-12-18T01:50:12Z")

</div>

If you agree that any “thing” inside of a `Vector{T}` is of type `T`, then my argument follows from the observation

```julia
julia> resize!(Union{}[], 1)
1-element Array{Union{},1}:
 #undef

```

It is also compatible with that `Union{} <: T` holds for any type `T`. So you can “put” `#undef` inside of any arrays:

```julia
julia> Union{} <: Some
true

julia> resize!(Some[], 1)
1-element Array{Some,1}:
 #undef

julia> Base._return_type(getindex, (Vector{Some}, Int))
Some

julia> ans == Union{Union{}, Some}
true

```

(Everything I said here just “kind of” makes sense.)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 18, 2019, 6:59am UTC](https://discourse.julialang.org/t/what-is-the-type-of-undef/32411/9 "2019-12-18T06:59:22Z")

</div>

> [@Qiyamah](#):
>
> Basically I am asking what is the type of field x in this case?

Generally, _values_ have types, and for your example there is no value yet (hence `#undef`). So `t.x` will raise an error, and `typeof(t.x)` cannot be executed as such.

So, in a sense, the question is meaningless. `#undef` was introduced precisely to signal this: it is not a value _per se_, just a way of signaling that there is no value.

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [December 18, 2019, 7:14am UTC](https://discourse.julialang.org/t/what-is-the-type-of-undef/32411/10 "2019-12-18T07:14:43Z")

</div>

you can think of the type as Int
