# How to create a \`Union\` value/literal?

**URL:** <https://discourse.julialang.org/t/how-to-create-a-union-value-literal/87880>\
**Category:** New to Julia\
**Created:** [September 27, 2022, 5:36pm UTC](https://discourse.julialang.org/t/how-to-create-a-union-value-literal/87880 "2022-09-27T17:36:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [September 27, 2022, 5:36pm UTC](https://discourse.julialang.org/t/how-to-create-a-union-value-literal/87880/1 "2022-09-27T17:36:44Z")

</div>

I’d like to initialize an array of type `Array{Union{Missing,Float64},2}` with a Float64 value. What’s the most succinct way?

If you were initializing an `Array{Float32,2}` with 3.14, you would be doing

```julia
a = fill(Float32(3.14), 2, 3)

```

By extension, I thought

```julia
a = fill(Union{Missing,Float64}(3.14), 2, 3)

```

would do the job, but it results in an error.

For that matter, I don’t know how to construct a `Union{Missing,Float64}` value from a `Missing` or from a `Float64`.

[This clearly shows that I don’t know what `Typename()` is.]

---

<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:** [September 27, 2022, 5:41pm UTC](https://discourse.julialang.org/t/how-to-create-a-union-value-literal/87880/2 "2022-09-27T17:41:35Z")

</div>

Values always have concrete types (e.g. Float64 or Missing). There is no such thing as a value with abstract type or Union type, and you can never construct an instance of an abstract or Union type. Instead, you could call the `arr = Array{...}(size)` constructor and then use `fill!(arr, 3.14)`.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 27, 2022, 6:05pm UTC](https://discourse.julialang.org/t/how-to-create-a-union-value-literal/87880/3 "2022-09-27T18:05:19Z")

</div>

```julia
julia> A = Array{Union{Missing,Float64},2}(undef, 10, 10)
10×10 Matrix{Union{Missing, Float64}}:
 missing missing missing missing … missing missing missing missing
 missing missing missing missing missing missing missing missing
 missing missing missing missing missing missing missing missing
 missing missing missing missing missing missing missing missing
 missing missing missing missing missing missing missing missing
 missing missing missing missing … missing missing missing missing
 missing missing missing missing missing missing missing missing
 missing missing missing missing missing missing missing missing
 missing missing missing missing missing missing missing missing
 missing missing missing missing missing missing missing missing

julia> fill!(A, 0.0)
10×10 Matrix{Union{Missing, Float64}}:
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

```

---

<div class="post-metadata">

**Author:** ![lbilli](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@lbilli](https://discourse.julialang.org/u/lbilli)\
**Post date:** [September 27, 2022, 6:05pm UTC](https://discourse.julialang.org/t/how-to-create-a-union-value-literal/87880/4 "2022-09-27T18:05:42Z")

</div>

Slightly convoluted but works:

```julia
julia> a = missings(Float32, 2, 3) .= 3.14
2×3 Matrix{Union{Missing, Float32}}:
 3.14 3.14 3.14
 3.14 3.14 3.14

```

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [September 28, 2022, 12:17am UTC](https://discourse.julialang.org/t/how-to-create-a-union-value-literal/87880/5 "2022-09-28T00:17:24Z")

</div>

> [@ryofurue](#):
>
> I’d like to initialize an array of type `Array{Union{Missing,Float64},2}` with a Float64 value. What’s the most succinct way?

```julia
julia> Union{Missing, Float64}[2.1 2.1; 2.1 2.1]
2×2 Matrix{Union{Missing, Float64}}:
 2.1 2.1
 2.1 2.1

```

This takes advantage of the syntax described in the manual [here](https://docs.julialang.org/en/v1/manual/arrays/#Typed-array-literals).

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [September 28, 2022, 2:43am UTC](https://discourse.julialang.org/t/how-to-create-a-union-value-literal/87880/6 "2022-09-28T02:43:34Z")

</div>

Thank you everyone for the solutions! I’ll use one or another of them in my code.

Just for fun, I’ve realized that I can use @CameronBieganek’s idea to construct a 2x3 array of `Union{Missing,Float64}` like this:

```julia
julia> elem = Union{Missing, Float64}[2.1] # 1-element vector
julia> a = repeat(transpose(repeat(elem,3)),2)
2×3 Matrix{Union{Missing, Float64}}:
 2.1 2.1 2.1
 2.1 2.1 2.1

```

With `repeat`, the 1-element vector looks as if it were a scalar. (The fact is, though, `repeat` doesn’t accept a scalar as its first element except for a character. )

@rdeits

> There is no such thing as a value with abstract type or Union type, and you can never construct an instance of an abstract or Union type.

This was the heat of my question. Thank you for elucidating!

It’s interesting, though, as @CameronBieganek points out, julia offers a special syntax

```julia
elem = Union{Missing, Float64}[2.1] # one-element vector

```

It’s _as if_ an instance of an abstract type were constructed.

If a special syntax to construct an abstract scalar(?) were provided, `fill` could be used, for example, like

```julia
a = fill(2.1::Union{Missing,Float64}, 2, 3) # or
a = fill(Union{Missing,Float64}(2.1), 2, 3) # or
. . .

```

---

<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:** [September 28, 2022, 4:13am UTC](https://discourse.julialang.org/t/how-to-create-a-union-value-literal/87880/7 "2022-09-28T04:13:03Z")

</div>

> [@ryofurue](#):
>
> julia offers a special syntax

Actually, that’s not special syntax, though it does look like it. Try:

```julia
julia> T = Union{Missing, Float64}
Union{Missing, Float64}

julia> @edit T[2.1]

```

and you’ll see that you’re just calling `getindex` (the function which is always called by indexing with `[]`), which has a built-in definition for _type_s. It’s a bit of a pun, but it’s certainly convenient.

If I wanted to do this in my own code, I’d just make a new function:

```julia
function my_fill(::Type{T}, value, dims) where {T}
   arr = Array{T}(undef, dims)
   fill!(arr, value)
end

```

This will be a bit faster than your `repeat` version, since it doesn’t waste any time constructing a 1-element vector only to throw it away.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 28, 2022, 4:46am UTC](https://discourse.julialang.org/t/how-to-create-a-union-value-literal/87880/8 "2022-09-28T04:46:27Z")

</div>

> [@rdeits](#):
>
> ```julia
> function my_fill(::Type{T}, value, dims) where {T}
> arr = Array{T}(undef, dims)
> fill!(arr, value)
> end
> 
> ```

This is exactly how `zeros`, `ones`, `trues`, and `falses` work.

> <https://github.com/JuliaLang/julia/blob/afb6c60d69a38e8a2442a0c7e87c47b8880ad294/base/array.jl#L581-L598>

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [September 28, 2022, 8:27am UTC](https://discourse.julialang.org/t/how-to-create-a-union-value-literal/87880/9 "2022-09-28T08:27:16Z")

</div>

Thanks, @rdeits and @mkitti ! Your `my_fill()` is of course exactly what I wanted!

That makes me wonder whether the standard `fill()` could be extended to take a type argument just as `zeros()` does:

```julia
julia> b = zeros(Union{Missing,Float64}, (2,3))
2×3 Matrix{Union{Missing, Float64}}:
 0.0 0.0 0.0
 0.0 0.0 0.0

```

Your `my_fill()` is a natural extension of `zeros()`:

```julia
julia> function my_fill(::Type{T}, value, dims) where {T}
   arr = Array{T}(undef, dims)
   fill!(arr, value)
end
julia> a = my_fill(Union{Missing,Float64}, 2.1, (2,3))
2×3 Matrix{Union{Missing, Float64}}:
 2.1 2.1 2.1
 2.1 2.1 2.1

```

When I first wanted to initialize an array with a non-zero value, I discovered `fill()` but wondered why it doesn’t take a type as its first argument. For example, if I wanted an Int32 array, I would write:

```julia
a = zeros(Int32, (2,3)) # works
b = fill(9, (2,3)) # Woops, I get Array{Int64} !
c = fill(Int32(9), (2,3)) # works.

```

Your `my_fill()` is pleasanter to use:

```julia
b = my_fill(Int32, 9, (2,3))

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 28, 2022, 8:34am UTC](https://discourse.julialang.org/t/how-to-create-a-union-value-literal/87880/10 "2022-09-28T08:34:50Z")

</div>

> [@ryofurue](#):
>
> ```julia
> b = my_fill(Int32, 9, (2,3))
> 
> ```

This syntax was rejected since it does not allow one to specify the array type.

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [November 19, 2024, 8:06am UTC](https://discourse.julialang.org/t/how-to-create-a-union-value-literal/87880/11 "2024-11-19T08:06:01Z")

</div>

For whoever found this thread interesting, I’ll add a little followup.

> [@mkitti](#):
>
> This is exactly how `zeros`, `ones`, `trues`, and `falses` work.

Revisiting this thread, I’ve just realized that the `missings()` function  
[lbilli](https://discourse.julialang.org/u/lbilli) uses conforms to the pattern of `zeros()` etc. It’s found in the `Missings` package.

Unfortunately (to me), the `OffsetArrays` don’t extend it as it does `zeros()` etc.:

```julia
using Missings
using OffsetArrays
a = zeros(Float64, 2, 3) # -> Matrix{Float64}
b = zeros(Float64, 0:1, -1:1) #-> OffsetArray(::Matrix{Float64}, 0:1, -1:1)
c = missings(Float64, 2, 3) # -> Matrix{Union{Missing, Float64}}
d = missings(Float64, 0:1, -1:1) # -> Error

```

I’m not saying that it’s difficult to add a method to the `missings()` function to support the 4th example above. I’m just writing this just for information to anybody who is interested.
