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
io: Document that io.ByteReader should return 0 when error is set. #20825
Comments
Go convention is that for a function or method returning (T, error), T is not to be considered if the error != nil unless otherwise documented, and we try to only document the exceptions so the documentation doesn't turn into stutter everywhere. But considering that io.Reader is such an exception and io.ByteReader looks similar in name, I could see the argument for calling out the behavior here explicitly. |
But we wouldn't document that the byte must be 0. Just that it's meaningless if error != nil. |
That sounds reasonable, since callers can't assume anything about the return value in that case. Is it okay to submit a CL? |
Go for it. Note that document CLs are some of the pickiest reviews, so it might take a few rounds of wordsmithing until it matches Go doc style. |
FYI, this is a duplicate of #11308. In my original CL, it originally worded it as "An error is reported only if no byte is available.", which I personally still prefer. |
Although CL mentioned in #11308 is well worded, this issue tries to make more of an implication. I think any possible confusion arises out of the fact that callers care about the value of the byte, not the error. Taking inspiration from math/big, one possibility is "The returned byte is valid but not defined if an error is reported." |
CL https://golang.org/cl/46950 mentions this issue. |
Is there a situation in which an io.ByteReader will return a nonzero byte value along with io.EOF? Code that relies on this assumption expects that a zero-value is implicitly returned at the end of a file, for example in utf8 rune-handling code.
All public standard library implementations of ReadByte return 0 on EOF. Additionally, the documentation for io.Reader suggests that implementations return certain combinations of values in different situations, like (0, EOF) at the end of an input stream, and (0, nil) only when len(p) == 0.
Seeing that io.ByteReader has consistent implementations across the standard library, can its documentation suggest that 0 be returned on EOF?
The text was updated successfully, but these errors were encountered: