Best way to define methods for SparseArray function based on eltype?

I have two methods I want to write for a function. I expect both of them to be passed two SparseVectors, but would like to keep it open for any type of Array. I want the compiler to pick between the methods based on the eltype, either an Integer or a Rational.

What is the best way to define these functions?

Does AbstractSparseArray inherit AbstractArray?

1 Like

you can use this structure as a format:

#for abstract types
function f(x::T) where T <: AbstractArray{T2,N} where {T2<:Integer,N}
              return 1
end
#for concrete types
function f(x::T) where T <: AbstractArray{Float64,N} where N
              return 2
end

if you are going to use only vectors, this is shorter:

#for abstract types
function f(x::T) where T <: AbstractVector{T2} where T2<:Integer
              return 1
end
#for concrete types
function f(x::T) where T <: AbstractVector{Float64}
              return 2
end

yes it does

2 Likes