I have a vector
a = Union{Misisng, Int}[1, 2, 3]
The all elements of a is Int
. Is there a way I can remove the Missing
element type in place?
some_function!(a) #change the element type in place
a
3-element Vector{Int64}:
1
2
3
I have a vector
a = Union{Misisng, Int}[1, 2, 3]
The all elements of a is Int
. Is there a way I can remove the Missing
element type in place?
some_function!(a) #change the element type in place
a
3-element Vector{Int64}:
1
2
3
Vector{Int}(Union{Missing, Int}[1, 2, 3])
Edit: Oops, missed that you wanted in-place.
No. In-place operations preserve the element type of the array, since they’re just setting elements - they don’t mess with the overall type of the array, only single elements. Other threads/objects may have a reference to this array as well, while expecting to be able to insert Missing
at their leisure, so such operations are (in general) not safe to do.
There is, actually: unsafe_wrap(Array, Ptr{Int}(pointer(a)), length(a))
makes a Vector{Int}
sharing the same data as a
, without any copies [ref]. It’s inplace in terms of memory, but doesn’t change the a
object itself.
Technically correct, but using unsafe
is well, unsafe. More precisely, I believe it is an implementation detail that a Vector{Union{Missing, Int}}
can be interpreted as a Vector{Int}
.
Thanks much for your reply. Would this unsafe wrap method have impact on gc
?
It is treated like any other allocated array and will break if the underlying implementation of arrays with Union
eltype changes.
Yes, that is right. In jupyter, the object drived by the unsafe wrap method just can’t be printed twice, the jupyter kernel would be broken.