-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: errors: expose Iser and Aser #39539
Comments
Any package could define those interfaces itself, of course. I'm not sure how compelling it is to define them in the standard library. I would not expect many packages to use them. |
While I agree that any package can define these symbols, exporting them from |
Simply implementing the interfaces seems like not enough if you are checking correctness. If you write a test, you'll check the whole end-to-end implementation, including the method signatures. One thing we could do is update the cmd/vet check to check the signature of an Is or As method on a type implementing error, like we do in the 'stdmethods' check for things like WriteByte. Both of those would seem better than exporting Iser/Aser. |
I missed this before, but xerrors exports While I agree such end-to-end tests are better, |
I'm pretty sure we didn't copy Wrapper over because all code should use errors.Unwrap. There's little need for it once you have Unwrap. We could make the same vet check check an Unwrap method on an error implementation too. It sounds like people are generally in favor of the vet check instead of exposing interfaces? Do I have that right? |
While I dislike that these interfaces only exist in documentation, the vet check would serve the same purpose as
Are we concerned with collisions, since these words are not use case specific, like |
Not really, because the check would only happen if you also implement Based on the discussion above, it sounds like this is a likely decline. |
Can this be exported for documentation purposes. Like how |
I think exporting a named interface weakens the abstraction, as the name of the interface matters in some cases unlike its anonymous counterpart (there was an issue about this, but I can't find it). Also, |
No change in consensus, so declined. |
Would you like me to submit a different proposal for the vet check, or does it make more sense to transmute this one? |
@carnott-snap I think a different proposal would be better. Thanks. |
@rsc: closing this one as well, did the process change, or were these two just missed? |
For me, the most compelling argument for an interface defined in this package is one of documentation. It's much easier to find on an exported interface than buried in the docs of Is(). But also, the argument for "you can just write a test for it" falls flat for me. I could just write a test for lots of things the compiler could do for me. But why? I guess I don't see why you wouldn't want it here? It would be more clear and discoverable. It would give one canonical definition of the interface. It gives you a place to anchor the docs. I understand that keeping an API small is desirable, but this interface already exists in the API of this package... It's just invisible. That's bad. Edit: One more thing that I think is important - by defining the interface in the package, we give it a name. Without a name, it's really hard to have a conversation about this interface, or reference it. Right now, you can't tell someone "oh, you need to implement errors.Iser to do that...." you have to say "oh you have to add a method to your type called The json.Marshaler example is perfect. How do you do custom json marshalling? Implement json.Marshaler. I can google for json.Marshaler. I can't google (easily) for |
Reopening to merge discussion on #45568. |
As I stated in #45568 given that errors expect these interfaces (or perhaps the word is not expects but allows, invites? sorry can't find the word in english) to be implemented in errors, it makes sense that it provides the code contract for them, as @natefinch says, this IS a compiled language, asking people to implement these interfaces in their packages is akin, for me, to ask them to implement This is my proposed implementation and naming for it #45568 (comment) |
@smasher164 Thanks, but I don't think it's ideal to reopen a closed proposal. The conversation gets complicated and it's harder to see what the decision was and why. I think it's preferable to open a new proposal, cite this one, and say "we should revisit this because of this new information that was not considered in the original discussion." Thanks. |
Gotcha, sorry about that. Should I close this issue and reopen the other one, or keep it as is? |
I think it would be fine to close this issue and reopen the other one. Thanks. |
The interfaces for
errors.Is
anderrors.As
seem stable and complete. Unfortunately, it requires reading through the godoc and use of a custom type to ensure that your custom error type implementsinterface{ As(interface{}) bool }
orinterface{ Is(error) bool }
.I propose exposing the following symbols:
And fixing up
errors.Is
anderrors.As
to document and use them:This allows
error
implementers to ensure that the interfaces are implemented correctly, while also cleaning uperrors
documentation and internal logic:That being said, the names
Iser
andAser
sound like fantasy characters and are not strictly required, since they do not exist today anderrors.Is
anderrors.As
work fine. Furthermore, I cannot think of a reason to pass, accept, or typecast these interfaces, so their usefulness is limited. That being said,json.Marshaler
suffers from similar issues, yet it exists.The text was updated successfully, but these errors were encountered: