-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
More helpful MethodErrors for deliberately unimplemented methods #7512
Comments
This makes a lot of sense. Could it also be used for depreciated functions? Right now I think they're still implemented but throw an exception or warning. It should also be available for modules, so packages can point out the replacements for old functions. In fact if all modules could implement |
Deprecated functions usually just prints a warning, and then perform what the old function was supposed to do. There might be something said for keeping a warning here, when the deprecation is fully removed though. It should definitely be extensible for modules. Would it be enough to just document how to |
IMO encountering minor differences in surface syntax or function names is entirely natural when learning a new language. I am not sure that hints like this belong in the language proper, especially since there are zillion other languages out there from which users can come to Julia, and it is nearly impossible to provide hints to everyone. For example, a former R user is likely to encounter difficulties different from someone coming from Python, etc. Wiki pages which enumerate differences between Julia and another languages would be more useful IMO. Eg similar to the table at the end of http://norvig.com/python-lisp.html |
+1 for this general approach. |
I love the idea of deliberately unimplemented methods. It would make the information from |
@tpapp It depends on how you look at it, but I would not consider Teaching language philosophy and differences is definitely not the target of this feature, but I don't think there are many functions where former R and Python users have different intentions when trying to call a undefined method in Julia. It is also possible to save some memory by not lazy loading this list, so I don't think there are going to be problems when 10 languages add their entire standard library with suggestions for Julia alternaltives. |
Sounds fine to me. @tpapp, I see what you mean, and I guess this is the difference between the purist and the popularist. Personally I want Julia to be popular and I think smoothing the initial "bump" of changing language is one of the most important ways of doing this. I gave an introductory presentation on Julia the other day and one of the thinks people liked most was what they got "in the box", eg.;
Strictly none of these are "part of the language" but it's convenient for them to come with it. Remember, the biggest reason (by far) that people use matlab is the IDE that comes with the language, not the language. |
One of the important things about shipping with something is that it tends to actually work, whereas add-ons have a much lower rate of working. |
I really like the idea of having hints, but it seems a bit messy to define these values directly in the code. Perhaps there should be a fixture file ( Each row of the For example:
|
@Aerlinger That saves some typing, but I don't think a special purpose parser makes things less messy. It will also be a new syntax to learn (even though it is intuitive). Julia code can also be placed in a separate file and loaded on demand. While we wait for someone to implement something like this, we should probably collect more examples on commonly missing methods. The format you use, can be treated as a vote. |
Per #7686, it would also be great to print "closest" matching methods for general MethodErrors, although defining the metric is going to be difficult. |
This is how I think it would be ideal to display: f(x::Float64, y::Float64) = println("ff")
f(x::Int, y::Int) = println("ii")
f(x::Float64, y::Int) = println("fi")
f(1, 1.0)
ERROR: `f` has no method matching f(::Int64, ::Float64)
Closest candidates are:
f(::Float64, ::Float64)
^~~~~~~~~
f(::Int, ::Int)
^~~~~
f(::Float64, ::Int)
^~~~~~~~~ ^~~~~ These underlines should indicate which arguments don't match. Even better would be if this utilizes colored output: The wrong types are printed red and the underlining |
I like @jhasse suggestion |
I suspect that this might cause more daunting messages for methods that are heavily overloaded. Consider: type A end
A() + A() This could be equally distant to all methods of |
Maybe remove all candidates where no arguments match at all? E. g. in my previous example: f(1, 1.0)
ERROR: `f` has no method matching f(::Int64, ::Float64)
Closest candidates are:
f(::Float64, ::Float64)
^~~~~~~~~
f(::Int, ::Int)
^~~~~ Then your example would result in no list, but |
That would never print suggestions for functions with fewer than two arguments – which might be ok. This is also pretty non-trivial to implement, but if you want to have a crack at it, please feel free. This would be awesome to have. |
Suggesting functions with different type signatures will not help in case of |
@ceving I don't understand what will not help. The suggestion in this issue is to print helpful messages together with MethodError, when we see cases where we (think we) know what the user wants to do. The message might be anything, and will certainly often suggest different functions (actually both my examples suggest to change function ( We know many programmers don't read the full manual before starting to program in Julia, and if we manage to save them of a few searches to figure out our api, that would save them a lot of time. If a common language defines something similar to |
Fixes #7512. This adds a hook for developers to add custom help messages when `MethodError`s get displayed. Feel free to bikeshed the naming of this method. This is particularly helpful in cases of intentionally unimplemented methods. I've also restructured the existing help messages in terms of this new system, using the `info` command to display them: ``` julia> Int("1") ERROR: MethodError: no method matching convert(::Type{Int64}, ::String) INFO: Cannot `convert` an object of type String to an object of type Int64. INFO: This may have arisen from a call to the constructor Int64(...), since type constructors fall back to convert methods. Stacktrace: [1] Int64(::String) at ./sysimg.jl:114 julia> min([1,2,3]) ERROR: MethodError: no method matching min(::Array{Int64,1}) INFO: Use minimum() to find the smallest element of an array. Closest candidates are: min(::AbstractArray{T1<:Real,N} where N, ::Real) where T1<:Real at deprecated.jl:55 min(::AbstractArray{T1<:Real,N} where N, ::AbstractArray{T2<:Real,N} where N) where {T1<:Real, T2<:Real} at deprecated.jl:55 min(::Any, ::Any) at operators.jl:404 ... julia> [1,2,3](1) ERROR: MethodError: no method matching (::Array{Int64,1})(::Int64) INFO: Use square brackets [] for indexing into arrays. ``` Feel free to bikeshed the name (`no_method_error_help`).
Define the table for deliberately unimplemented methods and their suggestionsdeliberately_unimplemented_methods = Dict( Extend the
|
One of the first problems you stumble upon when learning Julia, is the
MethodError
. When using the standard library, the reason is usually that you are doing something wrong, but the only thing the users sees isERROR: no method +(ASCIIString, ASCIIString)
. It would be very nice if the user would get a string with alternative suggestions.Currently Exceptions are printed with the help of
showerror(io::IO, err::MethodError)
in replutil.jlWe could have something like a table for lookup:
, but maybe we can find a better solution that also captures varargs functions.
The text was updated successfully, but these errors were encountered: