# How to deal with Vector{Function} for type inference?

**URL:** https://discourse.julialang.org/t/how-to-deal-with-vector-function-for-type-inference/98248
**Category:** General Usage
**Tags:** type, function, code\_warntype
**Created:** [May 3, 2023, 12:32pm UTC](https://discourse.julialang.org/t/how-to-deal-with-vector-function-for-type-inference/98248 "2023-05-03T12:32:15Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![chkwon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chkwon/32/3632_2.png) [@chkwon](https://discourse.julialang.org/u/chkwon)
#### Post date: [May 3, 2023, 12:32pm UTC](https://discourse.julialang.org/t/how-to-deal-with-vector-function-for-type-inference/98248/1 "2023-05-03T12:32:15Z")

</div>

I have the following two functions:

```julia
function foo1!(v::Vector{Float64}, b::Vector{Float64})
    for i in eachindex(v)
        v[i] = 2 * v[i] + 3 * b[i]
    end
end

function foo2!(v::Vector{Float64}, b::Vector{Float64})
    for i in eachindex(v)
        v[i] = 10 * v[i]^2 - 8 * b[i]
    end
end

```

I want to randomly apply one of these. Two slightly different ways:

```julia
function barA() 
    v = rand(100)
    b = rand(100)

    if rand() < 0.5 
        foo1!(v, b)
    else
        foo2!(v, b)
    end
end

function barB() 
    foos = Function[foo1!, foo2!]

    v = rand(100)
    b = rand(100)

    i = rand(1:2)
    foo!::Function = foos[i]
    foo!(v, b)
end

```

`barA()` has no problem. But with `barB()`:

```julia
@code_warntype barB()

```

 ![Screenshot 2023-05-03 at 8.33.40 AM](https://global.discourse-cdn.com/julialang/original/3X/7/0/705bdee0bebe993c5577230a7a9db7ac757e080c.png)

Is there any way to address the type stability of `barB()`?

---

<div class="post-metadata">

### Author: ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)
#### Post date: [May 3, 2023, 1:08pm UTC](https://discourse.julialang.org/t/how-to-deal-with-vector-function-for-type-inference/98248/2 "2023-05-03T13:08:11Z")

</div>

Check out [FunctionWrappers.jl](https://github.com/yuyichao/FunctionWrappers.jl).

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [May 3, 2023, 2:31pm UTC](https://discourse.julialang.org/t/how-to-deal-with-vector-function-for-type-inference/98248/3 "2023-05-03T14:31:04Z")

</div>

> [@chkwon](#):
>
> `@code_warntype barB()`

Note that in this case both barA() and barB() return `nothing`… if you add `return v` before closing the function, both implementations are type stable.  
Although you still have type instability in the function vector, I would first check if it is really a problem in your implementation before worrying too much about it…

---

<div class="post-metadata">

### Author: ![chkwon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chkwon/32/3632_2.png) [@chkwon](https://discourse.julialang.org/u/chkwon)
#### Post date: [May 3, 2023, 5:38pm UTC](https://discourse.julialang.org/t/how-to-deal-with-vector-function-for-type-inference/98248/4 "2023-05-03T17:38:44Z")

</div>

Thanks!

Just in case anyone wondered, here is what I did:

```julia
using FunctionWrappers
import FunctionWrappers: FunctionWrapper

function foo1!(v::Vector{Float64}, b::Vector{Float64})
    for i in eachindex(v)
        v[i] = 2 * v[i] + 3 * b[i]
    end
end

function foo2!(v::Vector{Float64}, b::Vector{Float64})
    for i in eachindex(v)
        v[i] = 10 * v[i]^2 - 8 * b[i]
    end
end

struct TypeStableStruct
    func::FunctionWrapper{Nothing, Tuple{Vector{Float64}, Vector{Float64}}}
end

const foo1_ = TypeStableStruct(foo1!)
const foo2_ = TypeStableStruct(foo2!)

evaluate_func(f::TypeStableStruct, a, b) = f.func(a, b)

function barC() 
        
    v = zeros(4)
    b = ones(4)

    foos_ = TypeStableStruct[foo1_, foo2_]

    i = rand(1:2)
    foo_! = foos_[i]
    evaluate_func(foo_!, v, b)
end

@code_warntype barC()

```

---

<div class="post-metadata">

### Author: ![chkwon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chkwon/32/3632_2.png) [@chkwon](https://discourse.julialang.org/u/chkwon)
#### Post date: [May 3, 2023, 5:43pm UTC](https://discourse.julialang.org/t/how-to-deal-with-vector-function-for-type-inference/98248/5 "2023-05-03T17:43:52Z")

</div>

Thanks for the comment. I am trying to speed up a large code and wanted to make it sure that everything is type-stable, so that I can focus on other part of code optimization with peace of mind. 🙂
