# Weird error message with sum(1,2)

**URL:** <https://discourse.julialang.org/t/weird-error-message-with-sum-1-2/54522>\
**Category:** New to Julia\
**Tags:** error-message\
**Created:** [February 3, 2021, 10:35am UTC](https://discourse.julialang.org/t/weird-error-message-with-sum-1-2/54522 "2021-02-03T10:35:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [February 3, 2021, 10:35am UTC](https://discourse.julialang.org/t/weird-error-message-with-sum-1-2/54522/1 "2021-02-03T10:35:51Z")

</div>

Could this be improved?

```julia
julia> sum(1,2)
ERROR: MethodError: objects of type Int64 are not callable

```

Note that `sum(1,2,3)` results in a much better `ERROR: MethodError: no method matching sum(::Int64, ::Int64, ::Int64)`

I know that thinking to all possible errors of a user is a infinite task but this seems pretty basic…

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 3, 2021, 10:44am UTC](https://discourse.julialang.org/t/weird-error-message-with-sum-1-2/54522/2 "2021-02-03T10:44:34Z")

</div>

If the `help?> sum` text could be written in a more compact form, it would be useful too. Currently there are extra empty lines taking a lot of real-estate in small terminal displays.

---

<div class="post-metadata">

**Author:** ![paulmelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulmelis/32/35063_2.png) [@paulmelis](https://discourse.julialang.org/u/paulmelis)\
**Post date:** [February 3, 2021, 10:53am UTC](https://discourse.julialang.org/t/weird-error-message-with-sum-1-2/54522/3 "2021-02-03T10:53:41Z")

</div>

> sum(f, itr)
> 
> Sum the results of calling function f on each element of itr

With the 2-argument version you’re using, e.g. `sum(1,2)`, the `1` is interpreted as a function to apply, hence the error message. You probably want `sum([1,2])`

Edit: note that none of the calls `sum(1, 2, ..., n)` exist for n \>= 3, hence the `no method matching sum(...)` error, the 2-argument version exists but does not sum its arguments as you assume, as shown above

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 3, 2021, 4:17pm UTC](https://discourse.julialang.org/t/weird-error-message-with-sum-1-2/54522/4 "2021-02-03T16:17:51Z")

</div>

@paulmelis, please consider the question from @sylvaticus, as coming from a very experienced/expert Julia user.

---

<div class="post-metadata">

**Author:** ![paulmelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulmelis/32/35063_2.png) [@paulmelis](https://discourse.julialang.org/u/paulmelis)\
**Post date:** [February 3, 2021, 4:57pm UTC](https://discourse.julialang.org/t/weird-error-message-with-sum-1-2/54522/5 "2021-02-03T16:57:22Z")

</div>

I don’t understand what you mean. The original question is about improving the error message, but the error shown is exactly describing what the problem is. Also, the call to `sum(1,2)` itself succeeds, but it then calls `1()` which doesn’t work and results in the error shown, i.e. similar to

```julia
julia> v=1; v()
ERROR: MethodError: objects of type Int64 are not callable

```

So the error comes from deeper than `sum()` itself, which would make it hard to improve at the original call site.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 3, 2021, 5:06pm UTC](https://discourse.julialang.org/t/weird-error-message-with-sum-1-2/54522/6 "2021-02-03T17:06:21Z")

</div>

In the worst case (less elegant), there could be a method like

```julia
sum(x<:Number, y<:Number) = error("good error message")

```

---

<div class="post-metadata">

**Author:** ![paulmelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulmelis/32/35063_2.png) [@paulmelis](https://discourse.julialang.org/u/paulmelis)\
**Post date:** [February 3, 2021, 5:07pm UTC](https://discourse.julialang.org/t/weird-error-message-with-sum-1-2/54522/7 "2021-02-03T17:07:56Z")

</div>

Why then not simply implement that version of `sum()`? I.e. `sum(x<:Number, y<:Number) = x+y`?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 3, 2021, 5:10pm UTC](https://discourse.julialang.org/t/weird-error-message-with-sum-1-2/54522/8 "2021-02-03T17:10:04Z")

</div>

Because then it would be really unclear what the behavior of `sum` should be when passed in 2 arguments that can both be called and added. `sum` Is made to function with a single collection of elements, not a vararg.

For example, imagine the quite reasonable type:

```julia
struct Polynomial
    coefs::Tuple
end
Base.(+)(p1::Polynomial, p2::Polynomial)=Polynomial(p1.coefs .+ p2.coefs)
function (f::Polynomial)(x) = evalpoly(x, x.coefs)

```

---

<div class="post-metadata">

**Author:** ![paulmelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulmelis/32/35063_2.png) [@paulmelis](https://discourse.julialang.org/u/paulmelis)\
**Post date:** [February 3, 2021, 5:20pm UTC](https://discourse.julialang.org/t/weird-error-message-with-sum-1-2/54522/9 "2021-02-03T17:20:44Z")

</div>

Ah, that’s your point. Well, I guess I’m not too fond of special-casing situations when it comes to error messages 🙂 I mean, you can use `@which sum(1,2)` to understand what call is actually made. Using `@less sum(1,2)` to look at the actual code points pretty clearly to the first argument being assumed to be a function.

Plus, this is posted in the first steps sub-forum, which made me assume this was a beginner question.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 3, 2021, 6:47pm UTC](https://discourse.julialang.org/t/weird-error-message-with-sum-1-2/54522/10 "2021-02-03T18:47:31Z")

</div>

> [@lmiq](#):
>
> `sum(x<:Number, y<:Number) = error("good error message")`

This error message should carefully considered, in that case. The worst kind of message for this is a `MethodError` with “No method matching”. I have seen that a couple of times, and it’s incredibly confusing.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 3, 2021, 7:22pm UTC](https://discourse.julialang.org/t/weird-error-message-with-sum-1-2/54522/11 "2021-02-03T19:22:29Z")

</div>

Yes. Could be something like `function sum is for collections, use add (+) for scalars.`

This raises the question on whether the error messages in base should evolved to something very informative and specific for each case, or not. I am not sure how old languages deal with this (is it necessarily good that the messages are short?)
