Can't make simple Streams over indirect projects work

Srikanth srikanth.adayapalam at oracle.com
Wed Aug 14 08:51:09 UTC 2019



On 28/07/19 8:02 PM, Ben Evans wrote:
> On Sun, 28 Jul 2019 at 13:22, Brian Goetz <brian.goetz at oracle.com> wrote:
>> This is not related to your streams issue, but don’t use __WithField, write a regular ctor.   WF is there for testing and will go away.
> OK!
>
>> Try rewriting your stream with all explicit types, including generic withesses,
>> To rule out type inference issues.
> This code:
>
> Stream<OptionalInt?> soi = opts.stream();
> ToIntFunction<OptionalInt?> f = (OptionalInt? o) -> {
>      if (o == null) return 0;
>      OptionalInt op = (OptionalInt)o;
>      return op.orElse(0);
> };
> IntStream sint = soi.mapToInt(f);
> int total = sint.reduce(0, (x, y) -> x + y);
>
> fails with:
>
> bevans$ javac -XDallowWithFieldOperator -XDallowGenericsOverValues
> javamag/Main4.java
> javamag/Main4.java:18: error: ')' expected
>          ToIntFunction<OptionalInt?> f = (OptionalInt? o) -> {
>                                                      ^
> javamag/Main4.java:18: error: : expected
>          ToIntFunction<OptionalInt?> f = (OptionalInt? o) -> {
>                                                         ^
> 2 errors
I believe the code snippet above compiles fine after this push: 
http://hg.openjdk.java.net/valhalla/valhalla/rev/7ec951ef532d

while the one below still fails to compile. I am looking into whether 
that is reasonable behavior or not.

I hope this unblocks you enough to continue your experiments.

Much appreciate the defect reports and your continued testing.

Thanks!
Srikanth
> whereas the type-inferred form:
>
> Stream<OptionalInt?> soi = opts.stream();
> ToIntFunction<OptionalInt?> f = o -> {
>      if (o == null) return 0;
>      OptionalInt op = (OptionalInt)o;
>      return op.orElse(0);
> };
> IntStream sint = soi.mapToInt(f);
> int total = sint.reduce(0, (x, y) -> x + y);
>
> fails with this:
>
> bevans$ javac -XDallowWithFieldOperator -XDallowGenericsOverValues
> javamag/Main4.java
> javamag/Main4.java:19: error: incomparable types: OptionalInt and <null>
>              if (o == null) return 0;
>                    ^
> javamag/Main4.java:18: error: incompatible types: incompatible
> parameter types in lambda expression
>          ToIntFunction<OptionalInt?> f = o -> {
>                                          ^
> 2 errors
>
> Thanks,
>
> Ben



More information about the valhalla-dev mailing list