Iterable::forEach not returning Iterable
Yuval Shavit
yshavit at akiban.com
Mon May 21 07:15:16 PDT 2012
On Sun, May 20, 2012 at 8:57 AM, Rémi Forax <forax at univ-mlv.fr> wrote:
> On 05/20/2012 02:21 PM, Tomasz Kowalczewski wrote:
> > I understand that forEach is eager and do not suggest it should be
> > changed (although I can imagine lazy versions on some specialized
> > collections working just fine for the user). This is rather an
> > argument towards something that potentially is more user friendly and
> > is based on the fluent interface pattern. Iterable::into is eager and
> > does return a collection which allows user to chain the calls further,
> > why not forEach then?
>
> because there is no type in Java corresponding to the values
> of side effects so the return value is void (as usual).
Many methods have "this" as the type+value of side effects:
StringBuilder.append's overloads are a perfect example. More generally, any
time I see a method that returns back "this" or any of the method's other
arguments, I take that as a pretty clear sign that the returned object has
been modified. Personally, I'd like to see *more* of that in libraries; for
instance, there are definitely times I've wished that Collections.sort
returned back the array I had just passed in.
More information about the lambda-dev
mailing list