Proposal: JDK-8148917 Enhanced-For Statement Should Allow Streams

forax at forax at
Thu Mar 21 15:48:28 UTC 2019

----- Mail original -----
> De: "Peter Levart" <peter.levart at>
> À: "Remi Forax" <forax at>, "Brian Goetz" <brian.goetz at>
> Cc: "core-libs-dev" <core-libs-dev at>
> Envoyé: Jeudi 21 Mars 2019 15:54:21
> Objet: Re: Proposal: JDK-8148917 Enhanced-For Statement Should Allow Streams

> On 3/21/19 2:17 PM, forax at wrote:
>> for some reason i was convinced that IntStream.iterator() was returning a
>> PrimitiveIterator.OfInt and not an Iterator<Integer>,
>> so yes, it will work but i don't understand why it's not BaseStream instead of
>> Stream<Object> that inherits from Iterable.
> I think it's the Iterable.forEach(Consumer<? super T>) that would clash
> with IntStream.forEach(IntConsumer), LongStream.forEach(LongConsumer)
> and DoubleStream.forEach(DoubleConsumer) if Iterable was a supertype of
> BaseStream.
> ...unless IntConsumer was made to extend Consumer<Integer>, LongConsumer
> was made to extend Consumer<Long> and DoubleConsumer was made to extend
> Consumer<Double>...
> I tried that and it (almost) compiles. The sole problem presents the
> following class:
>     public class LongSummaryStatistics implements LongConsumer,
> IntConsumer { ...
> So I had another idea. What if IntConsumer, LongConsumer and
> DoubleConsumer were all made to extend Consumer<Number> ?
> In that case LongSummaryStatistics would only have to override the
> method to disambiguate it.
> By doing that, the problem springs in hierarchy
> (Sink<T> extends Consumer<T>) where Sink.OfInt extends both
> Sink<Integer> and IntConsumer...
> Perhaps this could be fixed (as it is internal non-public API), but the
> same problem could exist in 3rd party code...

having a class that implements 2 consumers like IntConsumer and LongConsumer is hard to retrofit if as you said IntConsumer is a subtype of Consumer<Int> and LongConsumer is a subtype of Consumer<Long>, it means that the bridge accept(Object) has to dispatch to accept(int) and accept(long) at the same time 

A code like
 Object o = ...
 Consumer c = new LongSummaryStatistics(...);
shows the issue.

Having LongSummaryStatistics implementing IntConsumer was a mistake in retrospect.

so thanks, i've my answer why BaseStream can not implement Iterable until at least we have reified generics.

> Regards, Peter


More information about the core-libs-dev mailing list