# Eltypes of \`view\`s

**URL:** <https://discourse.julialang.org/t/eltypes-of-view-s/65606>\
**Category:** Performance\
**Tags:** eltype\
**Created:** [July 31, 2021, 3:52pm UTC](https://discourse.julialang.org/t/eltypes-of-view-s/65606 "2021-07-31T15:52:27Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 31, 2021, 3:52pm UTC](https://discourse.julialang.org/t/eltypes-of-view-s/65606/1 "2021-07-31T15:52:27Z")

</div>

Let’s say I have a vector `t = [1, 2, missing]`. And I take `sub_t = view(t, 1:2)`. I _know_ that no elements `sub_t` contain `missing` values.

Is there any way to make the `eltype` of `sub_t` be `Int` instead of `Union{Int, Missing}`?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 31, 2021, 3:58pm UTC](https://discourse.julialang.org/t/eltypes-of-view-s/65606/2 "2021-07-31T15:58:15Z")

</div>

since compiler can’t know, maybe you are looking for a way to remove `Missing` from the Union type returned by `eltype`?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 31, 2021, 4:04pm UTC](https://discourse.julialang.org/t/eltypes-of-view-s/65606/3 "2021-07-31T16:04:43Z")

</div>

Yeah. It’s definitely lying about the internal memory of the object, since the underlying vector still has room for the `Missing` mask.

I’m thinking of scenarios where functions have overly-restrictive type parameters

```julia
foo(x::AbstractVector{Int})
t = [1, 2, missing]
sub_t = view(t, 1:2)
foo(sub_t)

```

I’m not sure if just overloading the `eltype` is enough, right? It has to actually be in the type parameter.

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [July 31, 2021, 4:35pm UTC](https://discourse.julialang.org/t/eltypes-of-view-s/65606/4 "2021-07-31T16:35:31Z")

</div>

the thing though is that, as I imagine you know:

```julia
t = [1,2,missing]
sub_t = view(t, 1:2) # does not contain missing
t[1] = missing
sub_t[1] === missing

```

we had a similar issue with ScientificTypes but for slices (so not views), where if you take `t[1:2]` it would not be of eltype `Int`. For this you can apply `identity.(v)` to create a “tight” copy of it something like

```julia
t = [1,2,missing]
w = t[1:2] # has missing type still
z = (eltype(w) >: Missing && findfirst(ismissing, w) === nothing) ?
       identity.(w) : w

```

not a view though…

---

<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:** [July 31, 2021, 4:42pm UTC](https://discourse.julialang.org/t/eltypes-of-view-s/65606/5 "2021-07-31T16:42:15Z")

</div>

Since a `view` is quite literally a window into another objects’ memory and the original object can’t loose the ability to hold mixed types for arbitrary indices (it’s a property of the whole object after all), you won’t get rid of this without copying. Setting an index of a view means setting another index in the original object - the view is only there as a thin passthrough and index calculation.

If it were a property of each position however, it would be possible.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 31, 2021, 5:02pm UTC](https://discourse.julialang.org/t/eltypes-of-view-s/65606/6 "2021-07-31T17:02:16Z")

</div>

Yeah. That’s true.

If I were to implement my own type for this it would maybe disallow inputting `missing` values.

But it sounds like re-implementing `SubArray` or something similar would be the way to go for this, which is probably more work than I would want to go for at the moment.

---

<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:** [July 31, 2021, 5:15pm UTC](https://discourse.julialang.org/t/eltypes-of-view-s/65606/7 "2021-07-31T17:15:20Z")

</div>

You could also check every covered index for `missing` and only allow construction if truly all elements are non-missing. That would allow type stability, but doesn’t change the fact that the underlying array still has the Union.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 31, 2021, 5:17pm UTC](https://discourse.julialang.org/t/eltypes-of-view-s/65606/8 "2021-07-31T17:17:37Z")

</div>

Yeah that’s exactly what I’m doing, so I’m 100% sure there aren’t any `missing` values in the Array. This is like what `skipmissing` does [here](https://github.com/JuliaLang/julia/blob/master/base/missing.jl).

```julia
IteratorEltype(::Type{SkipMissing{T}}) where {T} = IteratorEltype(T)
eltype(::Type{SkipMissing{T}}) where {T} = nonmissingtype(eltype(T))

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 31, 2021, 10:41pm UTC](https://discourse.julialang.org/t/eltypes-of-view-s/65606/10 "2021-07-31T22:41:21Z")

</div>

This works, but the view is lost?

```julia
foo(x::AbstractVector{Int}) = dot(x,x)^3 # some example
t = [1, 2, missing]
sub_t = view(t, 1:2)
foo(sub_t) # Error
foo(Int.(sub_t)) # OK = 125

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 1, 2021, 2:15am UTC](https://discourse.julialang.org/t/eltypes-of-view-s/65606/11 "2021-08-01T02:15:29Z")

</div>

No. That creates a copy. The point of using a `view` is that I don’t have to make a copy of the data.

---

<div class="post-metadata">

**Author:** ![ivirshup](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivirshup/32/16364_2.png) [@ivirshup](https://discourse.julialang.org/u/ivirshup)\
**Post date:** [August 2, 2021, 3:07am UTC](https://discourse.julialang.org/t/eltypes-of-view-s/65606/12 "2021-08-02T03:07:10Z")

</div>

If you really wanted, you could probably get access to the data array via a pointer. Then you’d just take a view of the data array, as you don’t really care what values live in the masked segments. There isn’t a blessed way to do this ([https://github.com/JuliaLang/julia/issues/26681](https://github.com/JuliaLang/julia/issues/26681)).
