# Eltype changed during broadcast with missing

**URL:** <https://discourse.julialang.org/t/eltype-changed-during-broadcast-with-missing/71312>\
**Category:** General Usage\
**Tags:** broadcast, missing-values\
**Created:** [November 11, 2021, 7:42am UTC](https://discourse.julialang.org/t/eltype-changed-during-broadcast-with-missing/71312 "2021-11-11T07:42:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bgctw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bgctw/32/22050_2.png) [@bgctw](https://discourse.julialang.org/u/bgctw)\
**Post date:** [November 11, 2021, 7:42am UTC](https://discourse.julialang.org/t/eltype-changed-during-broadcast-with-missing/71312/1 "2021-11-11T07:42:14Z")

</div>

How do I tell julia to use the original eltype when broadcasting over a vector and a scalar?

In the following example I would like (and expected) the eltype of the result to be in all three cases `Union{Missing,Float64}`. However, the eltype depends on which subsets of the vector I take.

```julia
x = [1.0, 2.0, missing, missing]
y = 1.0
typeof(x.*y), typeof(x[1:2].*y), typeof(x[3:4].*y)
# (Vector{Union{Missing, Float64}}, Vector{Float64}, Vector{Missing})

```

This leads to complicated inferred types and further down to type instability when I use this kind of broadcasting. Especially harmful is the third case of accidentally only selecting Missings.

Currently, I use the cumbersome and case-specific:

```julia
res = x[1:2].*y
res_eltype = Union{eltype(x),typeof(y)}
convert(Vector{res_eltype}, res)::Vector{res_eltype}

```

What is the recommended way of telling Julia to use a type-stable expected broadcast type?  
Is there a solution to generalize to more complicated broadcasts?

---

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [November 13, 2021, 2:09am UTC](https://discourse.julialang.org/t/eltype-changed-during-broadcast-with-missing/71312/2 "2021-11-13T02:09:48Z")

</div>

Another option is to create the destination vector first and then use `broadcast!` (Julia 1.5.3):

```julia
julia> newx = Vector{Union{Missing,Float64}}(undef,2)
2-element Array{Union{Missing, Float64},1}:
 missing
 missing
julia> broadcast!(*,newx,x[1:2],y)
2-element Array{Union{Missing, Float64},1}:
 4.0
 8.0
julia> newx
2-element Array{Union{Missing, Float64},1}:
 4.0
 8.0

```

---

<div class="post-metadata">

**Author:** ![bgctw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bgctw/32/22050_2.png) [@bgctw](https://discourse.julialang.org/u/bgctw)\
**Post date:** [November 16, 2021, 7:49am UTC](https://discourse.julialang.org/t/eltype-changed-during-broadcast-with-missing/71312/3 "2021-11-16T07:49:45Z")

</div>

Thanks,  
The disadvantage, here, is that I need to provide the size of the vector in addition to the eltype.

Is there a method where I just state the eltype? Or a method where I can tell the broadcast to type-infer `Vector{Union{...}}` instead of `Union{Vector{...}}`?
