# SparseArray cannot store arbitrary type, but only types which support zero

**URL:** https://discourse.julialang.org/t/sparsearray-cannot-store-arbitrary-type-but-only-types-which-support-zero/64283
**Category:** General Usage
**Tags:** question
**Created:** [July 8, 2021, 3:24pm UTC](https://discourse.julialang.org/t/sparsearray-cannot-store-arbitrary-type-but-only-types-which-support-zero/64283 "2021-07-08T15:24:18Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [July 8, 2021, 3:24pm UTC](https://discourse.julialang.org/t/sparsearray-cannot-store-arbitrary-type-but-only-types-which-support-zero/64283/1 "2021-07-08T15:24:18Z")

</div>

I think this is a bug report to Julia, however I first would like to gather some thoughts.

Concretely, I cannot map over a SparseVector of Functions.

```julia
julia> a = SparseVector([1, 2])
2-element SparseVector{Int64, Int64} with 2 stored entries:
  [1] = 1
  [2] = 2

julia> fs = map(x -> y -> x+y, a)
2-element SparseVector{var"#32#34"{Int64}, Int64} with 2 stored entries:
  [1] = #32
  [2] = #32

julia> map(f -> f(1), fs)
ERROR: MethodError: no method matching zero(::Type{var"#32#34"{Int64}})
Closest candidates are:
  zero(::Union{Type{P}, P}) where P<:Dates.Period at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Dates/src/periods.jl:53
  zero(::T) where T<:Dates.TimeType at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Dates/src/types.jl:423
  zero(::AbstractIrrational) at irrationals.jl:148
  ...
Stacktrace:
 [1] _zeros_eltypes
   @ /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/SparseArrays/src/higherorderfns.jl:204 [inlined]
 [2] _noshapecheck_map(::var"#35#36", ::SparseVector{var"#32#34"{Int64}, Int64})
   @ SparseArrays.HigherOrderFns /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/SparseArrays/src/higherorderfns.jl:160
 [3] map(f::var"#35#36", A::SparseVector{var"#32#34"{Int64}, Int64})
   @ SparseArrays.HigherOrderFns /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/SparseArrays/src/higherorderfns.jl:143
 [4] top-level scope
   @ REPL[96]:1

```

Using julia 1.6.1

```julia
julia> versioninfo()
Julia Version 1.6.1
Commit 6aaedecc44 (2021-04-23 05:59 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-1065G7 CPU @ 1.30GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, icelake-client)
Environment:
  JULIA_EDITOR = code-insiders
  JULIA_NUM_THREADS = 

```

---

<div class="post-metadata">

### Author: ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)
#### Post date: [July 8, 2021, 3:35pm UTC](https://discourse.julialang.org/t/sparsearray-cannot-store-arbitrary-type-but-only-types-which-support-zero/64283/2 "2021-07-08T15:35:14Z")

</div>

> **I didn't read carefully :confused:**
>
> I’m not quite sure what you wanted to achieve but your map function essentially maps your entry to a function again. So your SparseVector now stores functions!
> 
> ```julia
> julia> a = SparseVector([1, 2])
> 2-element SparseVector{Int64, Int64} with 2 stored entries:
> [1] = 1
> [2] = 2
> 
> julia> fs = map(x -> (y -> "hello"), a)[1]("asdasdasdas")
> "hello"
> 
> julia> fs = map(x -> (y -> "hello"), a)[1](42)
> "hello"
> 
> ```
> 
> But mapping over SparseVectors in principle works:
> 
> ```julia
> julia> map(x -> x^2, a)
> 2-element SparseVector{Int64, Int64} with 2 stored entries:
> [1] = 1
> [2] = 4
> 
> ```

---

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [July 8, 2021, 3:36pm UTC](https://discourse.julialang.org/t/sparsearray-cannot-store-arbitrary-type-but-only-types-which-support-zero/64283/3 "2021-07-08T15:36:09Z")

</div>

it is intended that it stores functions.

I was expecting that SparseVector can deal with ANY Julia type. Is there anywhere a documentation saying that this is intentionally not the case?

---

<div class="post-metadata">

### Author: ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)
#### Post date: [July 8, 2021, 3:37pm UTC](https://discourse.julialang.org/t/sparsearray-cannot-store-arbitrary-type-but-only-types-which-support-zero/64283/4 "2021-07-08T15:37:43Z")

</div>

You’re right, I should read to the end 😃

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [July 8, 2021, 3:48pm UTC](https://discourse.julialang.org/t/sparsearray-cannot-store-arbitrary-type-but-only-types-which-support-zero/64283/5 "2021-07-08T15:48:02Z")

</div>

> [@schlichtanders](#):
>
> I was expecting that SparseVector can deal with ANY Julia type. Is there anywhere a documentation saying that this is intentionally not the case?

See [Sparse Arrays · The Julia Language](https://docs.julialang.org/en/v1/stdlib/SparseArrays/)

> Sparse arrays are arrays that contain enough zeros that storing them in a special data structure leads to savings in space and execution time, compared to dense arrays.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [July 8, 2021, 4:09pm UTC](https://discourse.julialang.org/t/sparsearray-cannot-store-arbitrary-type-but-only-types-which-support-zero/64283/6 "2021-07-08T16:09:05Z")

</div>

> [@schlichtanders](#):
>
> Concretely, I cannot map over a SparseVector of Functions.

Probably you want a dictionary/map container (e.g. the built-in `Dict` in Julia), not a `SparseVector`.

---

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [July 8, 2021, 4:10pm UTC](https://discourse.julialang.org/t/sparsearray-cannot-store-arbitrary-type-but-only-types-which-support-zero/64283/7 "2021-07-08T16:10:18Z")

</div>

thanks a lot, that explains completely, why it expects zero to exist
