-
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: io: add ReaderFunc and WriterFunc #47678
Comments
See also #47487 which tries to address this issue more generally. |
io.Reader/Writer both require something from/into which the reads/writes will occur. This is a natural semantic fit to an interface method, which must be defined on a type that would presumably serve that need. But functions are semantically stateless, so for this to make sense the function would necessarily have to close over some state, at which point you're acting as an ersatz method, anyway. (This is structurally different than e.g. http.HandlerFunc, which can fulfill its requirements statelessly.) Minor value delivered, substantial confusion generated. -1. |
This is not true. I’ve written Readers/Writers that block forever, generate data forever, always return errors, etc.
Closures can be entirely local; types with methods cannot. This can help readability. |
The example in the original post shows a function that closes over two variables, both on the lines immediately above it. Had that been declared as a new type, it would have been located very far from where it was actually used. That's a pretty big hit to readability. |
That's true. io.Discard is a stateless io.Writer; rand.Rand a stateless (ish) io.Reader. My language was too severe. Still, they're exceptional cases.
Yep, it can! I acknowledge the value this would deliver. I'm saying that value is outweighed by the costs to coherence. |
I'd prefer if we found a general solution like #47487 - but if that proposal is rejected, I think we should accept this one. |
Would also be useful for interface hiding, for example when using io.Copy family of functions.
Would be safe from an io.WriterTo implementation. And here's os having to do the same thing. https://github.com/golang/go/blob/master/src/os/file.go#L161-L167 |
I will put #47487 into the regular proposal minutes and move this one to hold. |
Placed on hold. |
I propose adding the following:
In tests, I find it common that I need an
io.Reader
orio.Writer
with specialized behavior. Usually, this is only necessary for a single test case within a large table-driven test. In order to have a customizedio.Reader
, I need to declare a concrete type to implement theRead
method. Since you can only declare a type with methods at the top-level, this results in the declaration potentially being located far away from the code actually using it.The proposed
ReaderFunc
(andWriterFunc
) allows me to create anio.Reader
that 1) requires no named declarations, and 2) have the specializedRead
behavior inlined with the place that actually plans on using it.Example usage:
This a library-specific workaround to the several proposed language changes (see #21670, #25860, #47487, and many others). Being a non-language change, it is presumably a more palatable change than a full language change. I personally can only recall ever needing something like #21670 for
io.Reader
andio.Writer
.This is very similar to the existing
http.HandlerFunc
type.\cc @josharian
The text was updated successfully, but these errors were encountered: