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.
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:
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?