Meaning and alternatives to "undef" when initializing vectors

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:

## 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.

6 Likes