Initiate an array of functions

I have func1 and func2.

When I create an array of the functions, like:

julia> A = [func1, func2]
2-element Vector{Function}:
func1 (generic function with 4 methods)
func2 (generic function with 4 methods)

That is good. But

julia> B = [func1]
1-element Vector{typeof(func1)}:
func1 (generic function with 4 methods)

How can I make B to be 1-element Vector{Function}?

This should do it:

B = Function[func1]

but why? this doesn’t seem very useful because Function is an abstract type. what are you trying to do


Thanks. That works, likewise:

B = Vector{Function}([func1])

But wonder why B = [func1] doesn’t get me a 1-element Vector{Function}?

Because Julia often tries to to give you the tightest type inference possible, as this often helps with performance. It is strange for functions because each function has its own type, so [func1] will become a Vector{typeof(func1)} which is technically more performant but at same time almost useless (because the vector can now only store references to the same single function instead of multiple distinct functions, defeating the purpose of a Vector). The same happens if you do [nothing] (the type will be Vector{Nothing}) but here it is more understandable because there is no intermediary type between Nothing and Any so the only reasonable types we could expect are either the almost useless Vector{Nothing} or the very inefficient Vector{Any}.