# What's a good way to get some value (doesn't matter what the value is) of a type \`T\` \`where T\`

**URL:** <https://discourse.julialang.org/t/whats-a-good-way-to-get-some-value-doesnt-matter-what-the-value-is-of-a-type-t-where-t/30474>\
**Category:** New to Julia\
**Created:** [October 30, 2019, 3:26am UTC](https://discourse.julialang.org/t/whats-a-good-way-to-get-some-value-doesnt-matter-what-the-value-is-of-a-type-t-where-t/30474 "2019-10-30T03:26:24Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [October 30, 2019, 3:26am UTC](https://discourse.julialang.org/t/whats-a-good-way-to-get-some-value-doesnt-matter-what-the-value-is-of-a-type-t-where-t/30474/1 "2019-10-30T03:26:24Z")

</div>

I am writing some program that requires a value from a type `T` but it doesn’t matter what this type is.

Is there a generic way to get some value of type `T` for whatever `T`? Happy to accept `T` for `isbits`.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [October 30, 2019, 3:32am UTC](https://discourse.julialang.org/t/whats-a-good-way-to-get-some-value-doesnt-matter-what-the-value-is-of-a-type-t-where-t/30474/2 "2019-10-30T03:32:23Z")

</div>

zero(T) would work for many types T

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [October 30, 2019, 3:32am UTC](https://discourse.julialang.org/t/whats-a-good-way-to-get-some-value-doesnt-matter-what-the-value-is-of-a-type-t-where-t/30474/3 "2019-10-30T03:32:57Z")

</div>

Forgot to mention, the default method is `zero(T)` for me. So nunbers are pretty much covered.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [October 30, 2019, 3:34am UTC](https://discourse.julialang.org/t/whats-a-good-way-to-get-some-value-doesnt-matter-what-the-value-is-of-a-type-t-where-t/30474/4 "2019-10-30T03:34:05Z")

</div>

rand(T)?

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [October 30, 2019, 3:34am UTC](https://discourse.julialang.org/t/whats-a-good-way-to-get-some-value-doesnt-matter-what-the-value-is-of-a-type-t-where-t/30474/5 "2019-10-30T03:34:35Z")

</div>

Currently I have method a method

```julia
some_elm(x::N) where N <: Number = zero(x)
some_elm(s::String) = ""

```

etc

Also experimenting with

`some_elm(::Type{T}) = T(0)`

and also `rand(Date)` doesn’t work

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [October 30, 2019, 3:34am UTC](https://discourse.julialang.org/t/whats-a-good-way-to-get-some-value-doesnt-matter-what-the-value-is-of-a-type-t-where-t/30474/6 "2019-10-30T03:34:52Z")

</div>

> [@xiaodai](#):
>
> rand(Date)

thanks. but also tried T\_T

---

<div class="post-metadata">

**Author:** ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)\
**Post date:** [October 30, 2019, 4:08am UTC](https://discourse.julialang.org/t/whats-a-good-way-to-get-some-value-doesnt-matter-what-the-value-is-of-a-type-t-where-t/30474/7 "2019-10-30T04:08:37Z")

</div>

How about Array{T}(undef, 0, 0)?

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [October 30, 2019, 4:17am UTC](https://discourse.julialang.org/t/whats-a-good-way-to-get-some-value-doesnt-matter-what-the-value-is-of-a-type-t-where-t/30474/8 "2019-10-30T04:17:17Z")

</div>

Pretty good. I can live with it now even though it doesn’t work for all types.

```julia
Vector{Type}(undef, 1)[1]
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] getindex(::Array{Type,1}, ::Int64) at ./array.jl:742
 [2] top-level scope at REPL[17]:1

```

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [October 30, 2019, 5:02am UTC](https://discourse.julialang.org/t/whats-a-good-way-to-get-some-value-doesnt-matter-what-the-value-is-of-a-type-t-where-t/30474/9 "2019-10-30T05:02:48Z")

</div>

One way is to change your program maybe so that it accepts a reference or a zero-dimensional array. Those can be constructed without any actual values:

```julia
julia> Ref{Int}()
Base.RefValue{Int64}(0)

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

julia> Array{DataType,0}(undef)
0-dimensional Array{DataType,0}:
#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:** [October 30, 2019, 6:26am UTC](https://discourse.julialang.org/t/whats-a-good-way-to-get-some-value-doesnt-matter-what-the-value-is-of-a-type-t-where-t/30474/10 "2019-10-30T06:26:50Z")

</div>

An alternative solution to `Array{T}` is

```julia
struct SomeValue{T}
    value::T
    SomeValue{T}() where T = new{T}()
end

function somevalue(T::Type)
    s = SomeValue{T}()
    return isdefined(s, :value) ? Some(s.value) : nothing
end

```

(you can also use `Base.RefValue` instead of `SomeValue`)

But this and `Array{T}`-based solution are both dangerous, as they do not call the constructor. For example:

```julia
struct OrderedPair
    small::Int
    large::Int
    OrderedPair(x, y) = new(min(x, y), max(x, y))
end

isok(pair::OrderedPair) = pair.small <= pair.large

sum(!isok, Vector{OrderedPair}(undef, 100)) # != 0

```

It might be better to restructure your program to avoid this pattern.

---

<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 30, 2019, 6:47am UTC](https://discourse.julialang.org/t/whats-a-good-way-to-get-some-value-doesnt-matter-what-the-value-is-of-a-type-t-where-t/30474/11 "2019-10-30T06:47:18Z")

</div>

I think that

```julia
gimme(::Type{T}) where T = (@assert isbitstype(T); Array{T}(undef)[])

```

should work.

---

<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:** [October 30, 2019, 7:55am UTC](https://discourse.julialang.org/t/whats-a-good-way-to-get-some-value-doesnt-matter-what-the-value-is-of-a-type-t-where-t/30474/12 "2019-10-30T07:55:56Z")

</div>

Regarding the `Array`-solutions: Consider e.g.

```julia
julia> Ref{Core.CodeInfo}()[];
ERROR: UndefRefError: access to undefined reference

```

This will throw an error for non-bitstypes, and avoids the foreign function calls from `Array`. I.e. it is a no-op if you just need some value for dispatch.

Generally, consider

```julia
julia> macro new(T)
       return Expr(:new, T)
       end

```

usable by e.g.

```julia
julia> ci=@new Core.CodeInfo;

julia> typeof(ci)
Core.CodeInfo

julia> ci
CodeInfo(
signal (11): Segmentation fault

```

This will always succeed in obtaining an object of your type, bypassing inner constructors. It basically obtains an allocation, zeros pointer fields, and sets the object header (containing type-info and gc flags).

As you saw from from the example, this leads to nullpointer fields that JITed code doesn’t expect, hence segfault instead of exception. This only is an issue for non-isbits fields, and julia code that may access the contents. Your call whether this falls into the realm of “don’t care about the contents”. Garbage collector and runtime functions (stuff implemented in C, like `objectid`) will deal with these impossible null-objects just fine.

(you can also construct truly toxic objects that crash the garbage collector; I am assuming that you don’t want them)

PS. This only works for concrete types.

Deciding whether an abstract type is “strongly inhabited” (whether instances can be created without cheating) or “weakly inhabited” (whether concrete subtypes exist) or completely uninhabited is quite non-trivial! Don’t expect a simple solution, this is probably undecidable (some typetheory/PL persons chime in here).

PPS. Afaiu these uninitialized objects should not introduce crashes in code that doesn’t throw an undef-error already, because we don’t allow llvm to speculatively load the pointer (we don’t set `dereferencable` flags). No idea whether that will change that in the future. If you really care, then you should ask yuyichao.
