----- Mail original ----- > De: "Peter Levart" <peter.lev...@gmail.com> > À: "Remi Forax" <fo...@univ-mlv.fr>, "Brian Goetz" <brian.go...@oracle.com> > Cc: "core-libs-dev" <core-libs-dev@openjdk.java.net> > 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, fo...@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... 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(...); c.accept(o); 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 regards, Rémi