# Should empty sum (and product) be defined?

**URL:** <https://discourse.julialang.org/t/should-empty-sum-and-product-be-defined/1872>\
**Category:** Internals & Design\
**Created:** [February 3, 2017, 4:01pm UTC](https://discourse.julialang.org/t/should-empty-sum-and-product-be-defined/1872 "2017-02-03T16:01:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [February 3, 2017, 4:01pm UTC](https://discourse.julialang.org/t/should-empty-sum-and-product-be-defined/1872/1 "2017-02-03T16:01:51Z")

</div>

```julia
julia> sum([1 2])
3

julia> sum([]) # from math = 0 "by convention"
ERROR: MethodError: no method matching zero(::Type{Any})
Closest candidates are:
  zero(::Type{Base.LibGit2.Oid}) at libgit2/oid.jl:88
  zero(::Type{Base.Pkg.Resolve.VersionWeights.VWPreBuildItem}) at pkg/resolve/versionweight.jl:80
  zero(::Type{Base.Pkg.Resolve.VersionWeights.VWPreBuild}) at pkg/resolve/versionweight.jl:120
  ...
 in _mapreduce(::Base.#identity, ::Base.#+, ::Base.LinearFast, ::Array{Any,1}) at ./reduce.jl:148
 in sum(::Array{Any,1}) at ./reduce.jl:229

julia> sum((1, 2))
3

julia> sum((1,))
1

julia> sum((,))
ERROR: syntax: unexpected ,

julia> sum(())
ERROR: StackOverflowError:
 in convert(::Type{Union{}}, ::Int64) at ./sysimg.jl:55 (repeats 80000 times)

```

I can see that maybe you want an error. Just not sure.

Not sure how you do the product, but it is defined as 1.

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [February 3, 2017, 4:14pm UTC](https://discourse.julialang.org/t/should-empty-sum-and-product-be-defined/1872/2 "2017-02-03T16:14:41Z")

</div>

This has already been discussed, see [https://github.com/JuliaLang/julia/issues/8092](https://github.com/JuliaLang/julia/issues/8092).

Note that

```julia
julia> sum(Int[])
0
```

Edit: The stackoverflow is probably a bug.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [February 3, 2017, 5:11pm UTC](https://discourse.julialang.org/t/should-empty-sum-and-product-be-defined/1872/3 "2017-02-03T17:11:23Z")

</div>

Note that

```julia
julia> sum(Int[])
0

```

I.e. it works when it is able to determine the type of the empty array.
