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

Peter Levart peter.levart at gmail.com
Thu Mar 21 14:54:21 UTC 2019



On 3/21/19 2:17 PM, forax at univ-mlv.fr 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 java.util.stream.Sink 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...

Regards, Peter



More information about the core-libs-dev mailing list