-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: io: "unimplemented" error return for WriterTo et al. #21904
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
Comments
Related to #16474 where I noted that there are many inefficient implementations of Another example is Lines 682 to 694 in c167928
This test is necessary since |
I should note that detecting whether a method is truly implemented or not requires actually calling that method, which may have side-effects (in the case of However, in all of the use cases I have personally seen, the method that is being attempted is always the first choice, and if it were not implemented, then there is a fallback. |
Personally I don't want to have to always have to ask everything whether it was lying when it said it could do something. Especially since the root problem of extending an arbitrary interface value also effects the proposed sentinel error so everything would always have to be careful to never add any extra context to it or code testing for the sentinel wouldn't get triggered. |
This is also an issue with io.EOF et al. The general question of how to produce distinguishable errors that can be wrapped is an interesting one, but I'd hate for us to give up on producing identifiable errors entirely until a perfect solution can be found. |
I see what you are saying, and we did learn that lesson for some other of the optional methods, like IsBoolFlag in package flag, or net.Error, but probably it doesn't make sense to redesign parts of package io in this way until we get to a comprehensive review of io as part of Go 2. |
I think this is not the right approach, as described at #16474 (comment) . I'm going to close this issue in favor of the discussion over there. |
Previous discussion: #21670 (comment)
io.Copy
takes anio.Reader
, but uses a type assertion to select an alternate implementation if the reader implementsio.WriterTo
. This pattern makes it difficult to write a function which wraps an arbitrary reader--if the wrapper just wraps theRead
method it erases the more efficient path implemented byWriteTo
, but there's no simple way to wrap theWriteTo
method only if present.There are a variety of features we might add to the language to better support this case, but there's a simpler one which doesn't require any language changes at all:
io.Copy
and other functions which probe for an optional method shouldn't assume that a type has a working version of a method just because it implements it.Concretely, this is a proposal that the start of
io.Copy
be changed to something like this:If
WriteTo
returns an error indicating that the method is unimplemented,Copy
will continue along as if that method were not present.Searching for non-test type assertions to
ReaderFrom
andWriterTo
in the stdlib, I find:There might be some other methods that should get the same treatment.
The text was updated successfully, but these errors were encountered: