Extending Collector to handle a post-transform
tim at peierls.net
Wed Jun 12 06:36:19 PDT 2013
On Tue, Jun 11, 2013 at 8:28 PM, Brian Goetz <brian.goetz at oracle.com> wrote:
> So, let's take a step back. Before we go all the way around the world to
>> "solve" this problem, I would like to (a) be convinced we have a problem
>> and (b) understand better what the problem is. So far, I'm not really
>> seeing how the current API is so terrible (and, having already considered
>> all the things suggested so far and more, it seems most cures are worse
>> than the disease, whatever that disease actually is.)
Collector<T, ?, R> is terrible because it raises questions in the user's
mind that are entirely avoidable. I know how to explain Collector<T, R> to
people: "It's a thing that collects elements of type T into an R." I don't
know what to say about Collector<T, X, R> or Collector<T, ?, R> without
having to offer excuses: "That middle parameter? It's this internal thing
that you don't have to know about, but you still have to acknowledge in the
type parameters, but don't worry, you can just use a wildcard. You'll
probably never to need to type it, in fact."
It's not enough that many common uses of Collector won't mention the type
at all, because people still need to look things up. It might be only a
tiny amount of effort to ignore an extra type parameter, but multiply this
by hundreds of method lookups per developer, and multiply this by tens of
thousands of developers, and you have a major annoyance.
And it's avoidable: We can't eliminate the ugly, but we can push it
elsewhere. It's already the case that users of Collector don't need to call
or even know about the Collector methods (they can *construct *a Collector
out of parts corresponding to those methods, but that's a different thing),
so it should not be a problem to push the ugly to another type that doesn't
appear in the signatures of methods called by regular users.
What are the reasons *not *to do that?
- It adds a trivial extra step in the implementation of Stream.collect
and friends -- hardly a showstopper.
- It introduces a special type that curious users will ask about:
"What's this funny method in Collector and what's the return type?" But
such folks will follow the ugly wherever it goes. They'll be OK; it's the
incurious ones who need the ugly moved a safe distance away.
More information about the lambda-libs-spec-observers