# Remove the redundant element type without coping the source data

**URL:** <https://discourse.julialang.org/t/remove-the-redundant-element-type-without-coping-the-source-data/89646>\
**Category:** New to Julia\
**Created:** [November 2, 2022, 6:43am UTC](https://discourse.julialang.org/t/remove-the-redundant-element-type-without-coping-the-source-data/89646 "2022-11-02T06:43:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [November 2, 2022, 6:43am UTC](https://discourse.julialang.org/t/remove-the-redundant-element-type-without-coping-the-source-data/89646/1 "2022-11-02T06:43:19Z")

</div>

I have a vector

```julia
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?

```julia
some_function!(a) #change the element type in place

a
3-element Vector{Int64}:
 1
 2
 3

```

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [November 2, 2022, 6:49am UTC](https://discourse.julialang.org/t/remove-the-redundant-element-type-without-coping-the-source-data/89646/2 "2022-11-02T06:49:00Z")

</div>

```julia
Vector{Int}(Union{Missing, Int}[1, 2, 3])

```

Edit: Oops, missed that you wanted in-place.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 2, 2022, 7:09am UTC](https://discourse.julialang.org/t/remove-the-redundant-element-type-without-coping-the-source-data/89646/3 "2022-11-02T07:09:47Z")

</div>

> [@Brian1](#):
>
> The all elements of a is `Int`. Is there a way I can remove the `Missing` element type 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.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [November 2, 2022, 11:33am UTC](https://discourse.julialang.org/t/remove-the-redundant-element-type-without-coping-the-source-data/89646/4 "2022-11-02T11:33:14Z")

</div>

> [@Brian1](#):
>
> The all elements of a is `Int`. Is there a way I can remove the `Missing` element type in place?

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](https://github.com/JuliaLang/julia/issues/27831#issuecomment-400932535)]. It’s inplace in terms of memory, but doesn’t change the `a` object itself.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [November 2, 2022, 1:20pm UTC](https://discourse.julialang.org/t/remove-the-redundant-element-type-without-coping-the-source-data/89646/5 "2022-11-02T13:20:00Z")

</div>

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}`.

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [November 9, 2022, 7:10am UTC](https://discourse.julialang.org/t/remove-the-redundant-element-type-without-coping-the-source-data/89646/6 "2022-11-09T07:10:09Z")

</div>

Thanks much for your reply. Would this unsafe wrap method have impact on `gc`?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 9, 2022, 7:51am UTC](https://discourse.julialang.org/t/remove-the-redundant-element-type-without-coping-the-source-data/89646/7 "2022-11-09T07:51:12Z")

</div>

It is treated like any other allocated array and will break if the underlying implementation of arrays with `Union` eltype changes.

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [November 9, 2022, 8:54am UTC](https://discourse.julialang.org/t/remove-the-redundant-element-type-without-coping-the-source-data/89646/8 "2022-11-09T08:54:26Z")

</div>

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.
