# Writing Explicitly-Typed Functions for all Vector{Float} or Vector{Number} / and missing?!

**URL:** <https://discourse.julialang.org/t/writing-explicitly-typed-functions-for-all-vector-float-or-vector-number-and-missing/8986>\
**Category:** General Usage\
**Created:** [February 11, 2018, 5:58am UTC](https://discourse.julialang.org/t/writing-explicitly-typed-functions-for-all-vector-float-or-vector-number-and-missing/8986 "2018-02-11T05:58:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivo\_welch](https://avatars.discourse-cdn.com/v4/letter/i/d2c977/32.png) [@ivo\_welch](https://discourse.julialang.org/u/ivo_welch)\
**Post date:** [February 11, 2018, 5:58am UTC](https://discourse.julialang.org/t/writing-explicitly-typed-functions-for-all-vector-float-or-vector-number-and-missing/8986/1 "2018-02-11T05:58:24Z")

</div>

dear julia experts—I would like to define a function with explicit typing, which works for all types of float vectors. Its output should be the same type as its input.

I know how to do this for one specific type, such as Float64.

```julia
julia> x64= [3.0, NaN, 2.0];

julia> function lag64( a::Vector{Float64}, n::Int=1 )::Vector{Float64};
          vcat( fill(NaN, n), a[1:(end-n)] ); end

julia> lag64( x64 )
3-element Array{Float64,1}:
 NaN
   3.0
 NaN

```

I could define the same function repeatedly (Float16, Float32, Float64, BigFloat), but this seems like a bad idea—if only because I would need to hand-propagate subsequent function changes. (And, ideally, I also want it to work with Float+missing.)

Here is a simple example (out of many) that I have tried:

```julia
julia> x32=Vector{Float32}( [1.0, NaN, -1.0] );

julia> function lagA( a::Vector{AbstractFloat}, n::Int=1 )::Vector{AbstractFloat};
            vcat( fill(NaN, n), a[1:(end-n)] ); end

```

```julia
julia> ## lag64( x32 ) ## of course not
## ERROR: MethodError: no method matching lag64(::Array{Float32,1})
julia> lagA( x32 )
ERROR: MethodError: no method matching lagA(::Array{Float32,1})
julia> lagA( x64 )
ERROR: MethodError: no method matching lagA(::Array{Float64,1})

```

Although my question is in terms of Float, it is really more generic.

how do I define functions that input not just one specific type, but a set of types; and which then return the same type—say, a `Vector{Number}` and return a `Vector{Number}` forced to be of the same type. When the function is called with a `Vector{Int16}`, it will return a `Vector{Int16}`. And, because Missing seems to become a quasi-basetype in terms of its importance, I need to write functions that grok base types with missing, too.

Advice appreciated. Julia is fun, but has a good learning curve, so apologies for so many questions.

/iaw

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [February 11, 2018, 6:53am UTC](https://discourse.julialang.org/t/writing-explicitly-typed-functions-for-all-vector-float-or-vector-number-and-missing/8986/2 "2018-02-11T06:53:07Z")

</div>

```julia
function lagfloat(v::Vector{T}) where T<:AbstractFloat
  someone(v)::Vector{T}
end

```

If you must be explicit. May be your code don’t to be

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 11, 2018, 7:07am UTC](https://discourse.julialang.org/t/writing-explicitly-typed-functions-for-all-vector-float-or-vector-number-and-missing/8986/3 "2018-02-11T07:07:37Z")

</div>

Use a method to determine the type of the element you want to fill with, and leave the rest to dispatch and promotion:

```julia
using Missings
filler(::Type{T}) where {T <: AbstractFloat} = T(NaN)
filler(::Type) = missing
lag(v::AbstractVector{T}, n = 1) where T = vcat(fill(filler(T), n), v[1:(end-n)])

```

then the following work fine:

```julia
julia> lag(Float32.([1, 2, 3]), 1)
3-element Array{Float32,1}:
 NaN32  
     1.0
     2.0

julia> lag([:a, :b, :c], 1)
3-element Array{Union{Missings.Missing, Symbol},1}:
 missing
 :a     
 :b     

```

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [February 11, 2018, 7:17am UTC](https://discourse.julialang.org/t/writing-explicitly-typed-functions-for-all-vector-float-or-vector-number-and-missing/8986/4 "2018-02-11T07:17:07Z")

</div>

```julia
obj = Vector{Float64}([3, NaN, 2])
function lag(obj::Vector{T}, n::Integer = 1) where T <: AbstractFloat
    @assert n < length(obj) "n must be less than the length of the vector."
    vcat(fill(NaN, n), obj[1:(end - n)])::Vector{T}
end

```

Alternatively,

```julia
using Missings
function otherlag(obj::Vector{T}, n::Integer = 1) where T <: Union{AbstractFloat, Union{AbstractFloat, Missing}}
    @assert n < length(obj) "n must be less than the length of the vector."
    vcat(fill(missing, n), obj[1:(end - n)])::Vector{Union{T,Missing}}
end

```
