Your GitHub search seems to find a lot of cases of functions named CloserFunc, but that don't, and shouldn't, have a Close method. Can you point us to a few existing examples of code that would benefit from these additions? Thanks.
This seems like two separate proposals which happen to compose together but which could be discussed independently.
I agree that the first part -- CloserFunc -- seems to be subsumable into all of the various proposals to allow more convenient use of isolated functions to implement interfaces, and so a decision on that should consider the outcome of a decision on those other proposals.
The WriterAndCloser part seems interesting in its own right though, and is similar to a situation I've encountered a small number of times: wrapping another function which returns an io.Reader and hooking an additional Close side-effect onto it, to encapsulate some additional cleanup that needs to happen when passing the result to a function which accepts any arbitrary io.ReadCloser.
When I most recently had that need I optimistically looked into the io package for something with a similar but not quite identical design as this proposal:
I considered generalizing it into a reusable library but found some interesting questions when considering use-cases beyond my immediate need:
What should happen if the given io.Reader is also already an io.Closer? Does the new Close method call both the close callback and the underlying reader's Close? If so, what happens if one of them fails and the other succeeds, or if both of them fail?
What about all of the other optional interfaces the given reader could possibly implement? I see that io.NopCloser itself already contains a special case for when the given reader is an io.WriterTo:
I assume that this hypothetical ReaderWithClose would need to do something similar in order to preserve the io.Copy optimization that io.WriterTo implies.
Of course I realize that I'm talking about readers while the original proposal was talking about writers, and so my details here are off-topic. But I'm sharing this because I think there are equivalent questions for io.WriterAndCloser (as proposed here) or io.WriterWithClose (a write variant of my ReaderWithClose), and because it seems to me that if this proposal were accepted for writers then it would make sense to accept it for readers too, for consistency/symmetry.
(The question of whether the second argument to this function should be just a function or an io.Closer of course amounts to re-asking the question about the io.CloserFunc part of this proposal; I don't feel strongly either way about this and am intending to focus only on the questions that seem to arise when hooking an extra Close behavior onto an existing object, regardless of the exact interface used to do so.)
can add CloserFunc and WriterAndCloser
The text was updated successfully, but these errors were encountered: