# Uninitialized ("random") value of bits type

**URL:** <https://discourse.julialang.org/t/uninitialized-random-value-of-bits-type/6197>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 2, 2017, 2:24pm UTC](https://discourse.julialang.org/t/uninitialized-random-value-of-bits-type/6197 "2017-10-02T14:24:26Z")\
**Posts on this page:** 4\
**Page:** 1

<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:** [October 2, 2017, 2:24pm UTC](https://discourse.julialang.org/t/uninitialized-random-value-of-bits-type/6197/1 "2017-10-02T14:24:26Z")

</div>

I need a “placeholder” value of a (parametric) bits type, for type stability, in certain branches of my code. Ie I need a function `f` such that `f(T) isa T`. When `T <: Number`, `zero(T)` works, but I need something more general.

I am not sure what the best way of doing this is though. I thought of the following:

```julia
any_value1(T) = reinterpret(T, Vector{UInt8}(sizeof(T)))

any_value2(T) = Vector{T}(1)[1]

any_value3(T) = unsafe_get(Nullable{T}())

```

All work, yet I pity my future self who is going to read any of them later on. Is there anything more idiomatic?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [October 2, 2017, 4:35pm UTC](https://discourse.julialang.org/t/uninitialized-random-value-of-bits-type/6197/2 "2017-10-02T16:35:43Z")

</div>

Can you use `local x::T` ?

```julia
function f(y)
       local x::Float64
       for i in 1:y
         x = 1.0 * i
       end
       x
end

```

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [October 2, 2017, 4:40pm UTC](https://discourse.julialang.org/t/uninitialized-random-value-of-bits-type/6197/3 "2017-10-02T16:40:16Z")

</div>

`Nullable` was exactly designed for that. In Julia 0.7, the performance of `Union{T, Void}` has been/will be improved so that it no longer kills performance.

---

<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:** [October 3, 2017, 11:20am UTC](https://discourse.julialang.org/t/uninitialized-random-value-of-bits-type/6197/4 "2017-10-03T11:20:36Z")

</div>

I am aware of this, I am looking for the language construct which can serve as a building block for things that make up something similar to, say, `Nullable`. The latter sidesteps the issue above by using incomplete initialization. One can of course use something similar, eg

```julia
struct AnyValue{T}
    value::T
    AnyValue{T}() where T = new{T}()
end
any_value4(T) = AnyValue{T}().value

```
