Type inference for a singleton of type Function

The reason you cannot push to the [times2] vector is this:

julia> typeof([times2])
Array{typeof(times2),1}

The array created with [times2] is specifically an array that is allowed to contain objects of type typeof(times2). This is in much the same way that an array of Ints may only store Ints and initializing [15] will create such a vector (you cannot then push 1.6 to it).

This works just fine:

julia> v = [times2]
1-element Array{typeof(times2),1}:
 times2 (generic function with 1 method)

julia> push!(v, times2)
2-element Array{typeof(times2),1}:
 times2 (generic function with 1 method)
 times2 (generic function with 1 method)

If you want a vector that can store generic Functions, you can declare it like so:

julia> v = Function[times2]
1-element Array{Function,1}:
 times2 (generic function with 1 method)

julia> push!(v, times_n)
2-element Array{Function,1}:
 times2 (generic function with 1 method)
 times_n (generic function with 1 method)
2 Likes