another example of why Iâm really looking forward to when we have function currying in 1.1.
Not sure leaving undefined references in your array is a good practice, however. This is what missing was made for (or nothing, if you want it to throw errors).
What I think you are saying is âYou canât do that because the OP wants the set of things which are considered ânullâ to be the empty setâ.â And Iâm saying: Julia provides us with objects which are specifically intended to be considered ânullâ, so why not take one of those as the only element in the set of things which should be considered ânullâ?
I only bother to mention it because (at least in my experience) itâs not a common practice for a package to provide (as output) arrays containing undefined references and expecting someone to handle that, but it is not so unusual for them to provide arrays contianing nothing or missing.
Of course, I have no idea what the OP is trying to do, so I donât know what the appropriate choice is here.
No I didnât. I said that you shouldnât (editedâŚ) consider everything about âleaving undefined references in your arrayâ being âbad practiceâ (thatâs what I specifically replied to). I never once mentioned what he want since he didnât mention and I donât want to guess. Iâm only saying that you shouldnât just see undefined reference and claim they should all be replaced by missing or nothing and I even agree that unless you need to handle Any (i.e. all types) missing or nothing could be fine.
Sorry for reviving this rather old thread. There has not been an accepted answer yet and I had to find an answer to exactly this question. My solution is the following set of 4 functions:
filter_undef!(x::AbstractVector) = deleteat!(x, Base.Fix1(!isassigned, x).(eachindex(x)))
filter_undef(x::AbstractArray) = deleteat!(x[:], Base.Fix1(!isassigned, x).(eachindex(x)))
function replace_undef(x::AbstractArray{T,N}, el::Tel = missing) where {T, N, Tel}
Tnew = Union{T, Tel}
xnew = Array{Tnew,N}(x)
xnew[Base.Fix1(!isassigned, x).(eachindex(x))] .= el
xnew
end
function replace_undef!(x::AbstractArray{T,N}, el::Tel = missing) where {T, N, Tel}
Tel <: T || throw("type of $el ($Tel) does not match eltype of array ($T)")
x[Base.Fix1(!isassigned, x).(eachindex(x))] .= el
x
end
This seems like an XY problem/asking the wrong question. #undef isnât a value, itâs a label saying âthis entry doesnât exist yetâ. Accessing these entries is undefined behavior, and they can be replaced by any other value at any time (e.g. if you create an uninitialized array of floats, youâll find a bunch of random numbers left over).
What you want to do instead is generating an array of some sentinel value (probably nothing), replacing some of the nothings, and then filtering out any nothings left behind.