# Vector of functions as kwarg

**URL:** <https://discourse.julialang.org/t/vector-of-functions-as-kwarg/103629>\
**Category:** New to Julia\
**Created:** [September 7, 2023, 8:07pm UTC](https://discourse.julialang.org/t/vector-of-functions-as-kwarg/103629 "2023-09-07T20:07:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leibniz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leibniz/32/217959_2.png) [@Leibniz](https://discourse.julialang.org/u/Leibniz)\
**Post date:** [September 7, 2023, 8:07pm UTC](https://discourse.julialang.org/t/vector-of-functions-as-kwarg/103629/1 "2023-09-07T20:07:25Z")

</div>

Good evening,

what is wrong with the following?

```julia
function foo(; b::Vector{Function} = [(x)->x*x])
    print(b[1](2))
end

foo()

```

`ERROR: MethodError: no method matching var"#foo#7"(::Vector{var"#8#10"}, ::typeof(foo)) `

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [September 7, 2023, 8:12pm UTC](https://discourse.julialang.org/t/vector-of-functions-as-kwarg/103629/2 "2023-09-07T20:12:24Z")

</div>

It needs a `<:` because

```julia
julia> [(x->x)] isa Vector{Function}
false

julia> [(x->x)] isa Vector{<:Function}
true

```

so

```julia
julia> function foo(; b::Vector{<:Function} = [(x)->x*x])
           print(b[1](2))
       end
foo (generic function with 1 method)

julia> foo()
4

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 7, 2023, 8:16pm UTC](https://discourse.julialang.org/t/vector-of-functions-as-kwarg/103629/3 "2023-09-07T20:16:55Z")

</div>

jar1 basically got it. Basically `[x->x]` is a `Vector` of a specific function by default. If you want a `Vector{Function}` you need to say `Function[x->x]`

```julia
julia> function foo(; b::Vector{Function} = Function[x->x*x])
           print(b[1](2))
       end
foo (generic function with 1 method)

julia> foo()
4

julia> [x->x]
1-element Vector{var"#24#25"}:
 #24 (generic function with 1 method)

julia> Function[x->x]
1-element Vector{Function}:
 #26 (generic function with 1 method)

```

---

<div class="post-metadata">

**Author:** ![Leibniz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leibniz/32/217959_2.png) [@Leibniz](https://discourse.julialang.org/u/Leibniz)\
**Post date:** [September 7, 2023, 8:55pm UTC](https://discourse.julialang.org/t/vector-of-functions-as-kwarg/103629/4 "2023-09-07T20:55:27Z")

</div>

Cheers guys!  
I assumed the problem was with the keyword-argument placing. Before, I tried

```julia
function foo( b::Vector{Function})

```

and it worked fine.  
So the error was with the default value assignment and had nothing to do with the argument type…

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [September 7, 2023, 9:04pm UTC](https://discourse.julialang.org/t/vector-of-functions-as-kwarg/103629/5 "2023-09-07T21:04:32Z")

</div>

That will also only work if you pass it a `Vector{Function}` and would fail with a one-argument literal like `[x->x]`. It can often be a best-practice to not worry about the exact types of function arguments.

The technical language that describes this problem is the term [“invariant parameters”](https://docs.julialang.org/en/v1/manual/faq/#Why-doesn't-it-work-to-declare-foo(bar::Vector%7BReal%7D)-42-and-then-call-foo(%5B1%5D)?).

---

<div class="post-metadata">

**Author:** ![dylanxyz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanxyz/32/36646_2.png) [@dylanxyz](https://discourse.julialang.org/u/dylanxyz)\
**Post date:** [September 7, 2023, 9:39pm UTC](https://discourse.julialang.org/t/vector-of-functions-as-kwarg/103629/6 "2023-09-07T21:39:38Z")

</div>

The issue here is that the array `[x -> x*x]` has a different type than `[x -> x*x, x -> 2x, ...]`.

A function in julia like `x -> x*x` has its own type, when you create an array with different types of elements, they are promoted to a common type, in case of functions, it is the abstract type `Function`. However, if there is only _one_ element, there’s no need to promote, so the array will have the type of that single function, which is _not_ the type `Function`.
