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

**URL:** <https://discourse.julialang.org/t/best-way-to-define-methods-for-sparsearray-function-based-on-eltype/34429>\
**Category:** New to Julia\
**Created:** [February 10, 2020, 6:19pm UTC](https://discourse.julialang.org/t/best-way-to-define-methods-for-sparsearray-function-based-on-eltype/34429 "2020-02-10T18:19:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![squirrel](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@squirrel](https://discourse.julialang.org/u/squirrel)\
**Post date:** [February 10, 2020, 6:19pm UTC](https://discourse.julialang.org/t/best-way-to-define-methods-for-sparsearray-function-based-on-eltype/34429/1 "2020-02-10T18:19:56Z")

</div>

I have two methods I want to write for a function. I expect both of them to be passed two `SparseVector`s, 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`?

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [February 10, 2020, 7:17pm UTC](https://discourse.julialang.org/t/best-way-to-define-methods-for-sparsearray-function-based-on-eltype/34429/2 "2020-02-10T19:17:36Z")

</div>

you can use this structure as a format:

```julia
#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:

```julia
#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

```

> [@squirrel](#):
>
> Does `AbstractSparseArray` inherit `AbstractArray` ?

yes it does
