-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: export Is and/or As in an interface #45568
Comments
Dup of #39539. The justification provided was
|
We can bikeshed the names, but I think it is pretty clear that code which relies on a specific definition of an interface should export that interface if it is not defined elsewhere. |
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 Is(error)bool to do that ...." 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 interface{Is(error)bool}. |
This comment has been minimized.
This comment has been minimized.
Has anything changed since #39539 was closed? |
The two compelling arguments against adding exported interfaces containing
I think that a prerequisite for reconsidering whether we should add exported definitions of these interfaces is either unambiguously good names for the interfaces or a compelling use case. |
well, the proposed alternative, #40356, has been largely ignored, so that may be contributing to the ongoing suffering. |
I would recommend testing that an error produces the correct results when used with |
I can test my implementation of Regarding the name: the fact that we cannot quickly think a name should not stop us, as this error handling method reaches further adoption (I, for instance, am using this more and more) we are talking about it and honestly it feels like trying to talk about Prince when he had a symbol for a name. "the thing that happens when you define an Let us leave this issue open for a few days and see if we can get more voices for and against it, I suspect the result will be different from last time as it has gained a bit more adoption. |
Wasn't there a spec change that ensured interface types are just method sets now? I would think that the argument in #39539 that naming it would weaken the abstraction doesn't apply anymore. |
No change in consensus, so declined. |
Currently the definition for the signature of a type implementing the interface for
Is
andAs
lives in a one-liner insideIs
andAs
respectively.Is: https://golang.org/src/errors/wrap.go?s=1170:1201#L49
As: https://golang.org/src/errors/wrap.go?s=1170:1201#L91
I propose these two be exported as proper interfaces instead for clarity and to ease use of tools like
goimpl
and akin.This would also clarify the above snippets (this is just an opinion, not a critique of the code quality)
Is would read "err supports Is"
As would read "err supports As"
The text was updated successfully, but these errors were encountered: