Skip to content

Improving MethodError message for anonymous functions #57319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

Bumblebee00
Copy link
Contributor

This pull request aims to resolve issue #56325

In this example, you get a MethodError for your anonymous method (::var"#3#4"):

vals = [5,6,10,20];
accumulate(vals; init=0) do (old, new)
           old + new
       end
ERROR: MethodError: no method matching (::var"#3#4")(::Int64, ::Int64)
The function `#3` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  (::var"#3#4")(::Any)
   @ Main REPL[2]:2

This can be confusing for beginner julia programmers, because in the case of a do block, it might even be non-obvious that you wrote a function. I changed the error message adding the words "anonymous function" and adding a tip for the number of arguments:

ERROR: MethodError: no method of the anonymous function var"#2#3" matching (::var"#2#3")(::Int64, ::Int64)
The function `#2` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  (::var"#2#3")(::Any)
   @ Main REPL[2]:2


Tip: the function `#2` was called with 2 arguments, but has only one method accepting 1 argument.

Of cousre if the function is not anonymous the error will not say "anonymous function" and if has more than one method will not display the tip:

vals = [5,6,10,20];

function f(x,y,z)
	x+y+z
end

function f(x,y,z,w)
	x+y+z+w
end
# f is a non anonymous function with more than one method, but
# both methods of f dont take two arguments and wn error will be trown
accumulate(f, vals; init=0)
ERROR: MethodError: no method matching f(::Int64, ::Int64)
The function `f` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  f(::Any, ::Any, ::Any, ::Any)
   @ Main REPL[3]:1
  f(::Any, ::Any, ::Any)
   @ Main REPL[2]:1

I have done this by modifying the files base/errorshow.jl and test/errorshow.jl

@nsajko nsajko added the error messages Better, more actionable error messages label Feb 9, 2025
@@ -373,6 +377,10 @@ function showerror(io::IO, ex::MethodError)
catch ex
@error "Error showing method candidates, aborted" exception=ex,catch_backtrace()
end
# if the function has only on method, print a Tip about the number of arguments to be used
Copy link
Contributor

@nsajko nsajko Feb 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# if the function has only on method, print a Tip about the number of arguments to be used
# if the function has only one method, print a tip about the number of arguments to be used

@nsajko
Copy link
Contributor

nsajko commented Feb 9, 2025

I think these are good changes, but IMO it might be better as two separate pull requests, seeing as it's really two unrelated changes:

  • hint about a function being anonymous
  • hint about the number of arguments for a function with only a single method

@Bumblebee00
Copy link
Contributor Author

that is a sensible opinion, i created two different pull requests for the two features

inkydragon pushed a commit that referenced this pull request Feb 26, 2025
If a MethodError arises on a anonyomous function, the words "anonymous
function" are printed in the error like so:
```julia
g=(x,y)->x+y
g(1,2,3)
```
```
ERROR: MethodError: no method of the anonymous function var"#5#6" matching (::var"#5#6")(::Int64, ::Int64, ::Int64)
The function `#5` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  (::var"#5#6")(::Any, ::Any)
   @ Main REPL[4]:1
```

See the [original pull
request](#57319) and
[issue](#56325) #56325
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error messages Better, more actionable error messages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants