-
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: generalize As to all types #33473
Comments
func (wrapped http.ResponseWriter, r *http.Request) { You wrote that twice but I don't understand what this means. There is no name for this function/method. What does this mean? How is it called? |
it's a stand-in for an http.HandlerFunc. A common use case is I want to log the request time for an http Handler. I would usually do something like:
thanks for your time. |
I see, thanks. The new |
I was thinking that errors would wrap other errors by implementing prototype implementation: https://play.golang.org/p/xhQF8aMcHkv |
There's a fair amount that we have yet to learn about |
I made the same proposal in my feeedback on the xerrors package: https://gist.github.com/carlmjohnson/d06cd8d10e0aef65f565a55cc57cd986 I think @rsc is correct that we can revisit the question of generalizing this after xerrors.Is/As has been in wider use. |
Based on the discussion above, I think we should probably decline this proposal and encourage filing a new proposal in a year or two based on our experience with errors.As. Putting this proposal on hold for a year or two would not make sense because the right thing to do in a year or two is likely to differ in the details - better to write a new proposal. Thoughts? |
sounds good to me. thank you for the discussion. I realized too that it's unclear what it means to expose underlying types. |
The errors proposal introduces a new paradigm for inspecting wrapped errors.
I love this new paradigm and I think it should be extended to any interface. The one that immediately comes to my mind that would benefit is http.ResponseWrapper.
http.ResponseWriter
interface has 3 methods, but there are 5 other methods that the underlying type can implement:http.CloseNotifier
,http.Flusher
,http.Hijacker
,io.ReaderFrom
,http.Pusher
There are 2 "easy" ways to address this: a wrapping type can either expose them directly, and if the underlying type doesn't implement them, the method becomes a no-op; or the wrapping type can expose the original ResponseWriter, but this may leak functionality.
This nested wrapping looks very similar to the errors problem. But what if the wrapper was written like this?
Some unknowns about this proposal:
reflect
but that would mean pulling in reflect pkg a lot more.I think the real power in the
As
method in how it allows the input type to define it's ownAs(interface{})
method. We could do without a generalized method and just have the paradigm that wrapping interfaces implement anAs
unwrapper. An implementation would look something like this:This is tedious and not future proof. However, a generalized method depends on reflection.
Maybe a hybrid approach could be take where libraries that don't want to pull in reflection can implement it manually like above. I would note that the current errors proposal depends on reflection and that it would probably be used just as often. I think that this also allows for optimizations in the future where reflection could potentially be removed and the interface be unchanged.
Some corollaries:
Is
andUnwrap
functions are superfluous, but I haven't totally thought through that.Isn't
errors.Is(err, syscall.EADDRNOTAVAIL)
the same as:As(err, syscall.EADDRNOTAVAIL)
if err sufficiently implementsAs(type interface)
?The text was updated successfully, but these errors were encountered: