-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Improving MethodError message for anonymous functions #56325
Comments
Just a nitpick, but anonymous functions can have multiple methods. There might be a nicer way to add them, but this works at least: julia> f = x -> x*x
#1 (generic function with 1 method)
julia> (::typeof(f))(x::Int) = -x
julia> f
#1 (generic function with 2 methods)
julia> f(3.0)
9.0
julia> f(3)
-3 But otherwise I agree that names like |
Hello everyone @nsajko @fonsp @mikmoore, this is my first time working on julia codebase and I'd like to tackle this issue! I will write here what I came up just to do a sanity check with someone more experienced. So the code responsible for MethodError display is in the file function showerror(io::IO, ex::MethodError)
# other code here...
if is_arg_types
print(io, "no method matching invoke ")
else
print(io, "no method matching ")
end
buf = IOBuffer()
iob = IOContext(buf, io) # for type abbreviation as in #49795; some, like `convert(T, x)`, should not abbreviate
show_signature_function(iob, Core.Typeof(f))
show_tuple_as_call(iob, :function, arg_types; hasfirst=false, kwargs = isempty(kwargs) ? nothing : kwargs)
str = String(take!(buf))
str = type_limited_string_from_context(io, str)
print(io, str)
# other code here...
end Anonymous function and non anonymous ones have different types, as you can see here: function f1(x)
x+1
end
f2 = x->x+1
typeof(f1) # typeof(f1) (singleton type of function f1, subtype of Function)
typeof(f2) # var"#7#8" so I thought we could check in
causing the same error with a non anonymous function instead works as before:
So is this a legit method of implementing this? If yes I will continue implementing also the other suggestion about closest candidates, number of arguments and the tips, and write tests for everything. |
okay I did the pull request implementing also the other suggestions, let me know what you all think! |
I splitted the two features in two different pull request |
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
Context: improving error experience for beginner Julia programmers.
In this example, you get a
MethodError
for your anonymous method (::var"#3#4"
):The problem is that my
do
syntax used(old, new)
(a single tuple argument) instead ofold, new
(two integer arguments):But the error is difficult to understand!
Ideas
I have seen this pattern more often (
MethodError
on an anonymous function that I defined), and I think we could make meaningful improvements to this error message! Ideas:var"#3#4"
is not so useful for beginners. (In the case of ado
-block, it might even be non-obvious that you wrote a function.)It could be phrased as advice (when the situation is simple enough):
The text was updated successfully, but these errors were encountered: