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