Multiple dispatch

Is it possible to do a dispatch on a parameter of the types

  • Vector{Float64} and
  • Vector{Vector{Float64}}

?

E.g.

foo(v::Vector{<:Number}) = sum(v)
foo(vv::Vector{<:Vector}) = sum(foo, vv)

So this is a yes? Does a range count as Vector in this context?

Yes if types are different you can dispatch on them. A range should be an AbstractVector I think. You can check in the REPL (I am on mobile right now) with:

julia> (1:5) isa AbstractVector

So you’d need a bit different types then what I wrote to make it work with ranges.

e.g.

foo(v::AbstractVector{<:Number}) = sum(v)
foo(vv::AbstractVector{<:AbstractVector}) = sum(foo, vv)
2 Likes