Binary heap type problems

You specified the element type to be a Array{Pair{Int64,Function},1}, which is probably not what you intended. Try to use just Pair{Int,Function} and see if that works.

Every function has its own type with supertype Function, which allows for dispatch on function type and various optimizations. You can probably imagine that knowing the exact type of a function at compile time helps enormously with inference and optimization of functions that take functions as arguments and use them on some sorts of data.

The problem you ran into is, that you created an array with a single {Int, Functio} pair for initialization. The array, upon its creation, tries to take on the most specific type that holds the values you supplied, which happenes to be {Int,typeof(f1)}. (This is most often what you want, say, with numbers, but it may seem weird with functions indeed)
Try creating an array with two different functions initially and see for yourself! Good luck :slight_smile:

Edit: also note, that you can create arrays with explicit type specification by prepending the type to the array literal:

julia> [1=>(+), 2=>(-)]
2-element Vector{Pair{Int64, Function}}:
 1 => (+)
 2 => (-)

julia> [1=>(+), 2=>(+)]
2-element Vector{Pair{Int64, typeof(+)}}:
 1 => (+)
 2 => (+)

julia> Pair{Int,Function}[1=>(+), 2=>(+)]
2-element Vector{Pair{Int64, Function}}:
 1 => (+)
 2 => (+)
```
2 Likes