Can't make simple Streams over indirect projects work

Remi Forax forax at univ-mlv.fr
Tue Jul 30 16:19:03 UTC 2019


You have hit 2 different bugs :)

first, there is a type inference bug,
  ToIntFunction<OptionalInt?> mapper = o -> 0;
o is inferred as OptionalInt instead of OptionalInt?

then there is a grammar bug
  ToIntFunction<OptionalInt?> mapper = (OptionalInt? o) -> 0;
the parser choke on the '?' when declaring the type of the lambda parameter.

cheers,
Rémi

----- Mail original -----
> De: "Ben Evans" <benjamin.john.evans at gmail.com>
> À: "Brian Goetz" <brian.goetz at oracle.com>
> Cc: "valhalla-dev" <valhalla-dev at openjdk.java.net>
> Envoyé: Dimanche 28 Juillet 2019 16:32:46
> Objet: Re: Can't make simple Streams over indirect projects work

> 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
> 
> 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