# Meaning and alternatives to "undef" when initializing vectors

**URL:** <https://discourse.julialang.org/t/meaning-and-alternatives-to-undef-when-initializing-vectors/40759>\
**Category:** New to Julia\
**Created:** [June 4, 2020, 8:07pm UTC](https://discourse.julialang.org/t/meaning-and-alternatives-to-undef-when-initializing-vectors/40759 "2020-06-04T20:07:55Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 4, 2020, 8:07pm UTC](https://discourse.julialang.org/t/meaning-and-alternatives-to-undef-when-initializing-vectors/40759/1 "2020-06-04T20:07:56Z")

</div>

It is not that I have problems using this, but particularly for didactic purposes I feel I need a clarification:

Whenever I initialize a vector of, lets say, integers, using, for example

```julia
x = Vector{Int64}(undef,3)

```

There seems to be no other option to be put in the place of `undef`. I understand what `undef` is, but the syntax seems to suggest that something else could be used there to initialize the vector with specific values.

Thus, I have two questions:

1. In the initialization of numerical arrays (at least), that syntax accepts anything else in the place of `undef`? If so, what?
2. If not, is there any profound reason to maintain the syntax with that redundancy? Could it be adapted to accept, for example, `Vector{Int64}( index -> f(index), 3 )` to initialize a vector with a specific function of the indexes of the vector or anything else?

Just to clarify, I do not think that any of that would be needed. I am more concerned in understanding the reason behind that syntax, perhaps to explain to students.

Alternatively, wouldn’t be nice to have the possibility of initializing an array of `undef` values using something like

```julia
x = undef(Float64,3)

```

that would look consistent with the `zeros(3)` and `ones(3)` options.

---

<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:** [June 4, 2020, 8:21pm UTC](https://discourse.julialang.org/t/meaning-and-alternatives-to-undef-when-initializing-vectors/40759/2 "2020-06-04T20:21:02Z")

</div>

> [@lmiq](#):
>
> In the initialization of numerical arrays (at least), that syntax accepts anything else in the place of `undef` ? If so, what?

The only other option I know of in Base is `I`:

```julia
julia> Matrix{Float64}(I, 3, 3)
3×3 Array{Float64,2}:
 1.0 0.0 0.0
 0.0 1.0 0.0
 0.0 0.0 1.0

```

Edit: You need `using LinearAlgebra` to get `I`

---

<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:** [June 4, 2020, 8:21pm UTC](https://discourse.julialang.org/t/meaning-and-alternatives-to-undef-when-initializing-vectors/40759/3 "2020-06-04T20:21:52Z")

</div>

Oh, looking at `methods(Matrix{Float64})`, it looks like you can use `missing` as an initializer:

```julia
julia> Matrix{Union{Float64, Missing}}(missing, 3, 3)
3×3 Array{Union{Missing, Float64},2}:
 missing missing missing
 missing missing missing
 missing missing missing

```

and likewise with `nothing`.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 4, 2020, 8:23pm UTC](https://discourse.julialang.org/t/meaning-and-alternatives-to-undef-when-initializing-vectors/40759/4 "2020-06-04T20:23:59Z")

</div>

Yes, but in that case we have to add the `Union{}` stuff there. It is a very niche use, I think (I’ve never seen the use of that in numerical computing).

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 4, 2020, 8:29pm UTC](https://discourse.julialang.org/t/meaning-and-alternatives-to-undef-when-initializing-vectors/40759/5 "2020-06-04T20:29:13Z")

</div>

Didn’t know of that, and, in any case, it doesn’t work here ☹

```julia
julia> Matrix{Float64}(I, 3, 3)
ERROR: UndefVarError: I not defined

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 4, 2020, 8:31pm UTC](https://discourse.julialang.org/t/meaning-and-alternatives-to-undef-when-initializing-vectors/40759/6 "2020-06-04T20:31:12Z")

</div>

You need

```julia
using LinearAlgebra 

```

to import `I`.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 4, 2020, 8:36pm UTC](https://discourse.julialang.org/t/meaning-and-alternatives-to-undef-when-initializing-vectors/40759/7 "2020-06-04T20:36:02Z")

</div>

And what kind of stuff is `I`? meaning, how to implement something else if we wanted to?

```julia
julia> typeof(I)
UniformScaling{Bool}

```

---

<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:** [June 4, 2020, 8:38pm UTC](https://discourse.julialang.org/t/meaning-and-alternatives-to-undef-when-initializing-vectors/40759/8 "2020-06-04T20:38:28Z")

</div>

Try `@edit Matrix{Float64}(I, 3, 3)` and you’ll see that there’s nothing special about `I` in particular, except that someone has gone ahead and implemented:

```julia
## Matrix construction from UniformScaling
function Matrix{T}(s::UniformScaling, dims::Dims{2}) where {T}
    A = zeros(T, dims)
    v = T(s.λ)
    for i in diagind(dims...)
        @inbounds A[i] = v
    end
    return A
end
Matrix{T}(s::UniformScaling, m::Integer, n::Integer) where {T} = Matrix{T}(s, Dims((m, n)))

```

You can do exactly the same thing: define some `struct MyCustomInitializer ...` and then implement `Matrix{T}(::MyCustomInitializer, ...)` to do whatever behavior you want to provide.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 4, 2020, 8:40pm UTC](https://discourse.julialang.org/t/meaning-and-alternatives-to-undef-when-initializing-vectors/40759/9 "2020-06-04T20:40:25Z")

</div>

I is a struct designed to behave like an identity matrix of whatever size is needed in context. You would implement it by making a strut that stores no data, and has correctly implemented routines for all the operations you wanted it to support.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 4, 2020, 8:42pm UTC](https://discourse.julialang.org/t/meaning-and-alternatives-to-undef-when-initializing-vectors/40759/10 "2020-06-04T20:42:39Z")

</div>

That clarifies a lot. Thank you.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 4, 2020, 10:13pm UTC](https://discourse.julialang.org/t/meaning-and-alternatives-to-undef-when-initializing-vectors/40759/11 "2020-06-04T22:13:43Z")

</div>

But would it make sense to let

```julia
Array{Int, 2}(5, 3, 3)

```

behave like `fill(5, 3, 3)`? It just seems like `undef` and `I` and `missing` are special-cased, and I don’t understand why it doesn’t work for ‘everything’?

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [June 4, 2020, 11:00pm UTC](https://discourse.julialang.org/t/meaning-and-alternatives-to-undef-when-initializing-vectors/40759/12 "2020-06-04T23:00:36Z")

</div>

See issue:

> <https://github.com/JuliaLang/julia/issues/34775>
>
> I find the syntax for creating uninitialized arrays a bit verbose, while there a…re nice and short options for almost all other common cases of creating arrays:
> 
> \`\`\`julia
> \# compare to
> Float64\[1, 2, 3, 4, 5\]
> zeros(Float64, 10)
> ones(Float64, 10)
> fill(1.0, 10)
> \`\`\`
> 
> But for uninitialized arrays you always have to use curly bracket syntax if I'm correct:
> 
> \`\`\`julia
> v = Vector{Float64}(undef, 10)
> \`\`\`
> 
> How about one of these two alternatives, which both seem to be available:
> 
> \`\`\`julia
> v = Float64\[undef, 10\]
> arr = Int32\[undef, 3, 4, 5\]
> 
> \# or
> 
> v = undef(Float64, 10)
> arr = undef(Int32, 3, 4, 5)
> \`\`\`
> 
> The second one is actually easy to get via:
> 
> \`\`\`julia
> (::UndefInitializer)(T::Type, dims::Vararg{Int}) = Array{T}(undef, dims...)
> \`\`\`
