-
-
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
Add warn about usage of applicable and hasmethod #51260
base: master
Are you sure you want to change the base?
Conversation
This commit adds a warning about the functions `applicable` and `hasmethod`. A user reading the current documentation can think that if those functions return `true`, we can call the method without errors. However, it is not always the case and should be clearly stated. Closes JuliaLang#51258
LGTM! |
This feels a bit overly verbose to me at least. I don't really understand how you can infer from the current docstring that the function would not throw any exception. Would you assume that because Maybe the original docstring could be slightly elaborated on what
I don't really understand what this means. |
It is a docstring. IMHO, too much documentation is better than too little, which is the case.
One new user might assume that if Please, do not assume that everyone will have a deep understanding about methods, types, and multiple dispatch. Julia is getting more popular everyday. I heard many times from newcomers that Julia feels too complicated. In fact, this usage of
From my perspective, it is the expected result that the user calling However, if it is not clear enough, do you have a suggestion? |
Re the example, while deliberate fallback methods with an error are rare (and I think discouraged), many functions will try to iterate anything, and fail:
Perhaps this idea (accepting |
Perfect, your example is another important scenario why we really should describe much better what I add that fallback example because it seems somewhat common in API definitions:
What I proposed is the most compact text I can do today sorry :) At least I showed it to a student who had just started learning Julia, and he understood what was happening. I am all ears for suggestions on how to improve this warning. |
I'm using that mechanism in RequiredInterfaces.jl to give good error messages when some interface is not completely implemented, which is IMO much better than a generic julia> using RequiredInterfaces
julia> abstract type MyInterface end
julia> @required MyInterface begin
foo(::MyInterface)
end
foo (generic function with 1 method)
julia> struct MyImplementor <: MyInterface end
julia> foo(MyImplementor())
ERROR: NotImplementedError: The called method is part of a fallback definition for the `MyInterface` interface.
Please implement `foo(::MyInterface)` for your type `T <: MyInterface`.
Stacktrace:
[1] foo(::MyImplementor)
@ Main ./none:0
[2] top-level scope
@ REPL[7]:1 This would be first I'm hearing about this being discouraged. |
It's been called an antipattern in the past, IIRC https://www.oxinabox.net/2020/04/19/Julia-Antipatterns.html#notimplemented-exceptions In any case, LGTM |
Just because one blogpost says one particular version of this is bad, doesn't mean the entire concept is bad. A If you want to discuss this, I'd be happy to take it to the issues of RequiredInterfaces.jl - sorry for highjacking the PR! |
Actual usecases for |
I am of the belief that if there is confusion stemming from a particular piece of documentation, it is better to improve that documentation so the confusion doesn't arise in the first place, instead of leaving it unchanged and adding sizeable sections for everyone's interpretation of the confusing docs. The core issue here seems to be confusion about what the word "applicable" means in Julia. So to fix this, the correct way would in my mind be to add a few words to define that word. |
This commit adds a warning about the functions
applicable
andhasmethod
. A user reading the current documentation can think that if those functions returntrue
, we can call the method without errors. However, it is not always the case and should be clearly stated.Closes #51258