Weird error message with sum(1,2)

Could this be improved?

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…

1 Like

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.

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

1 Like

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

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> 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.

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

sum(x<:Number, y<:Number) = error("good error message")
2 Likes

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

1 Like

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:

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

Ah, that’s your point. Well, I guess I’m not too fond of special-casing situations when it comes to error messages :slight_smile: 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.

2 Likes

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.

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?)