Question

What is the best way in Julia to check whether an array entry is #undef?

Example:

julia> a = Array(Vector,2)
julia> isdefined(a[1])  # fails
julia> isempty(a[1])    # fails
Was it helpful?

Solution

You can push the access into isdefined by using isdefined(a, 1) instead of isdefined(a[1]):

julia> a = Array(Vector,2);

julia> a[2] = {10}
1-element Array{Any,1}:
 10

julia> a
2-element Array{Array{T,1},1}:
 #undef 
    {10}

julia> isdefined(a[1])
ERROR: access to undefined reference
 in getindex at array.jl:246

julia> isdefined(a, 1)
false

julia> isdefined(a, 2)
true
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top