Can't make simple Streams over indirect projects work

Ben Evans benjamin.john.evans at gmail.com
Sun Jul 28 14:32:46 UTC 2019


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