# Array of functions behaves differently when it is of length 1 vs longer

**URL:** <https://discourse.julialang.org/t/array-of-functions-behaves-differently-when-it-is-of-length-1-vs-longer/84996>\
**Category:** General Usage\
**Tags:** type, function\
**Created:** [July 29, 2022, 7:27pm UTC](https://discourse.julialang.org/t/array-of-functions-behaves-differently-when-it-is-of-length-1-vs-longer/84996 "2022-07-29T19:27:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexpkeil1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexpkeil1/32/35692_2.png) [@alexpkeil1](https://discourse.julialang.org/u/alexpkeil1)\
**Post date:** [July 29, 2022, 7:27pm UTC](https://discourse.julialang.org/t/array-of-functions-behaves-differently-when-it-is-of-length-1-vs-longer/84996/1 "2022-07-29T19:27:14Z")

</div>

I want to do something where I can call different functions on the same value, and that number of functions varies depending on how I use it. I’ve been using an array of functions to accomplish this, but I am getting an error when that array is length 1.

As an example `bar` and `foo` both operate on floats, and `foobar` loops over functions to print out the values of each of those functions when they are applied to the same float.

```julia
function bar(a::Float64)
 a
end

function foo(a::Float64)
 a+1.0
end

function foobar(functionlist::Array{Function})
  x=1.0
  for f in functionlist
    println(f(x))
  end
end

```

Example:

```julia
foobar([foo,bar])

```

yields

```julia
2.0
1.0

```

However, using a vector that only has length one (as follows), gives an error:

```julia
foobar([foo])

```

```julia
 ERROR: MethodError: no method matching foobar(::Vector{typeof(foo)})
Closest candidates are:
  foobar(::Array{Function}) at REPL[3]:1
Stacktrace:
 [1] top-level scope

```

Of course `typeof(foo)` is `Function`, so I can’t see why this isn’t working.

I can remove explicit typing of the `foobar` argument and this all works, but that doesn’t feel right to me.

Is this intended behavior? If it is, is there a better way to accomplish what I’m doing than just abandoning typing in this instance?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 29, 2022, 7:32pm UTC](https://discourse.julialang.org/t/array-of-functions-behaves-differently-when-it-is-of-length-1-vs-longer/84996/2 "2022-07-29T19:32:08Z")

</div>

you either need `foobar(functionlist::Array{<:Function})` or `foobar(Function[foo])` Julia types are covariant.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 29, 2022, 7:37pm UTC](https://discourse.julialang.org/t/array-of-functions-behaves-differently-when-it-is-of-length-1-vs-longer/84996/3 "2022-07-29T19:37:17Z")

</div>

> [@alexpkeil1](#):
>
> `functionlist::Array{Function}`

One you create an array with a single function, the array type is the function type, which is unique for each function. And in that signature you are expecting a an array with the abstract function signature.

Use `Array{<:Function}`

Which indicates that the array can be of any subtype of function. Check this:

```julia
julia> f(a::Array{Function}) = 1
f (generic function with 1 method)

julia> f([sin, cos])
1

julia> f([sin, sin])
ERROR: MethodError: no method matching f(::Vector{typeof(sin)})
Closest candidates are:
  f(::Array{Function}) at REPL[1]:1
Stacktrace:
 [1] top-level scope
   @ REPL[3]:1

julia> f([sin])
ERROR: MethodError: no method matching f(::Vector{typeof(sin)})
Closest candidates are:
  f(::Array{Function}) at REPL[1]:1
Stacktrace:
 [1] top-level scope
   @ REPL[4]:1

julia> typeof([sin, sin])
Vector{typeof(sin)} (alias for Array{typeof(sin), 1})

julia> typeof([sin, cos])
Vector{Function} (alias for Array{Function, 1})

julia> f(a::Array{<:Function}) = 2
f (generic function with 2 methods)

julia> f([sin])
2

julia> Function[sin, sin]
2-element Vector{Function}:
 sin (generic function with 13 methods)
 sin (generic function with 13 methods)

julia> [sin, sin]
2-element Vector{typeof(sin)}:
 sin (generic function with 13 methods)
 sin (generic function with 13 methods)

```

---

<div class="post-metadata">

**Author:** ![alexpkeil1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexpkeil1/32/35692_2.png) [@alexpkeil1](https://discourse.julialang.org/u/alexpkeil1)\
**Post date:** [July 29, 2022, 7:40pm UTC](https://discourse.julialang.org/t/array-of-functions-behaves-differently-when-it-is-of-length-1-vs-longer/84996/4 "2022-07-29T19:40:37Z")

</div>

I marked the other one as a solution due to the longer explanation, but you’ve got it, too. Thanks!
