# \`sum\` syntax rule differs between Julia and \`JuMP\`

**URL:** <https://discourse.julialang.org/t/sum-syntax-rule-differs-between-julia-and-jump/116125>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, syntax\
**Created:** [June 23, 2024, 11:38pm UTC](https://discourse.julialang.org/t/sum-syntax-rule-differs-between-julia-and-jump/116125 "2024-06-23T23:38:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [June 23, 2024, 11:38pm UTC](https://discourse.julialang.org/t/sum-syntax-rule-differs-between-julia-and-jump/116125/1 "2024-06-23T23:38:00Z")

</div>

```julia
a = [1, 2, 3, 4]
sum(a[n] for n in 1:0) # ERROR: MethodError: reducing over an empty collection is not allowed; consider supplying `init` to the reducer

using JuMP
m = Model()
@constraint(m, sum(a[n] for n in 1:0) >= 0) # It's OK.

```

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [June 23, 2024, 11:47pm UTC](https://discourse.julialang.org/t/sum-syntax-rule-differs-between-julia-and-jump/116125/2 "2024-06-23T23:47:10Z")

</div>

Correct. In normal Julia you need to pass the `init` keyword:

```Julia
julia> a = [1, 2, 3, 4]
4-element Vector{Int64}:
 1
 2
 3
 4

julia> sum(a[n] for n in 1:0; init = 0)
0

```

We have this set by default in JuMP because we know that people really want `0.0` when they have an empty summation.

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [June 23, 2024, 11:59pm UTC](https://discourse.julialang.org/t/sum-syntax-rule-differs-between-julia-and-jump/116125/3 "2024-06-23T23:59:51Z")

</div>

Thanks! Nice setting!
