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

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> 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)



1 Like