Skip to content
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

Closed
smasher164 opened this issue Jun 28, 2017 · 7 comments
Closed

io: Document that io.ByteReader should return 0 when error is set. #20825

smasher164 opened this issue Jun 28, 2017 · 7 comments
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@smasher164
Copy link
Member

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?

@bradfitz
Copy link
Contributor

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.

@bradfitz
Copy link
Contributor

But we wouldn't document that the byte must be 0. Just that it's meaningless if error != nil.

@bradfitz bradfitz added this to the Go1.10 milestone Jun 28, 2017
@bradfitz bradfitz added the NeedsFix The path to resolution is known, but the work has not been done. label Jun 28, 2017
@smasher164
Copy link
Member Author

That sounds reasonable, since callers can't assume anything about the return value in that case. Is it okay to submit a CL?

@bradfitz
Copy link
Contributor

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.

@dsnet
Copy link
Member

dsnet commented Jun 28, 2017

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.

See http://golang.org/cl/15256/

@smasher164
Copy link
Member Author

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."

@gopherbot
Copy link

CL https://golang.org/cl/46950 mentions this issue.

@golang golang locked and limited conversation to collaborators Jun 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants