-
-
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
[FR] NotImplementedError
, to communicate user-extendable but not-yet-implemented API
#50196
Comments
I have implemented this concept as part of a package now, you can find it here: RequiredInterfaces.jl. It works pretty much exactly as described in this issue, while also allowing implementors of an interface to check that they've at least implemented the correct methods of an interface. Please do check out the documentation if this is of interest to you. There are some aspects that (sadly) don't work, mostly due to limitations in Julia, which is expanded on in the docs. |
Was just exploring the many uses of In Julia, another common pattern is to have a fallback method with generic types that may attempt to convert to a more specific type. But if the more specific method hasn't been defined for whatever reason, you can end up with a stack overflow - so a A I think it might be helpful for the constructor to accept the same arguments as a Perhaps:
A usability catch here is possibly ensuring people don't overwrite the fallback method while attempting to fix the error. EDIT: Perhaps if we want to indicate an abstract type as an interface, could also do:
|
I don't necessarily see If packages want to make their own I'm afraid the rest of my comment might be going a little off-base from your intent and proposal, but I already wrote it so here it is: Some users already "abuse" If we can arrive at a clear and agreeable convention as to when a Again, maybe this last part is talking past this proposal and isn't relevant. |
Yes, that is the idea, as it is also implemented in RequiredInterfaces.jl. That's also exactly the semantic difference -
The functionality you're thinking of is I agree that the misuse of |
For some context, C#/.NET has The proposal here is fairly similar, except that because of how Julia works, the interface/type definer can throw the exception as a fallback method rather than the interface implementer having to manually throw it as boilerplate. |
This is more of a comment on the inverse of the Relating to dispatch/methods/composition/interfaces, there seem to be 2 main kinds of errors : A. This method doesn't exist and shouldn't/won't/can't exist because XYZ And The appeal of "abusing" MethodErrors is that sometimes there are methods which are known by the author to not work (case A). A different approach would be some kind of a Ultimately I think these are subtly orthogonal issues, and |
The issue with |
Currently, defining some form of API like so:
always throws a
MethodError
. From a user-perspective, it's hard to find out whetherbar
was supposed to be implemented by a package they're using or not - it could very well just be a bug, since allMethodError
communicates is "There is no method matching this". Since this can bubble up from deep in some library, it'd be better for users to be able to say "I got thisNotImplementedError
", which also makes it clear to package maintainers that they forgot to implement some method that is required of the interface they claim to implement.This feature request/proposal is aimed at solving this issue, by giving package authors & Base an option to communicate to users "There is no fallback definition, since your type should implement this". This is done with a new error type,
NotImplementedError
, which is defined like so:and used like so:
This allows the distinction between intended-to-be-extended API (
bar(::Foo, ::Int)
) and this-is-supposed-to-error (any other signature onbar
, which throws aMethodError
).To be discussed
The details of what exactly
NotImplementedError
should contain, since this version with just two strings is IMO too bare-bones, as it requires discipline/good grasp of the intended API to be able to create the string directly.Why is this needed?
This error and variations on it are widespread throughout the ecosystem and I think Base could benefit from having some fallback definitions like the one above as well, to give much more informative error messages when subtyping e.g.
<: AbstractArray
or other abstract types in Base.The text was updated successfully, but these errors were encountered: