I have some code that compiles with javac 1.8.0_92:
public final class Either<L, R> {
// ...
private final L l;
private final R r;
// ...
public <T> T join(final Function<L, T> f, final Function<R, T> g) {
Preconditions.checkNotNull(f);
Preconditions.checkNotNull(g);
return which == LeftOrRight.LEFT ?
f.apply(l) :
g.apply(r);
}
public Optional<L> left() {
return join(Optional::of, x -> Optional.empty());
}
// ...
}
However, with javac 1.8.0_45, some extra types are required (L):
public Optional<L> left() {
return join(Optional::<L>of, x -> Optional.<L>empty());
}
As you can imagine, this causes issues for packages that a user builds from source.
Why is this?
Is this a bug with that particular build of Java?
Yes, this is the JDK bug where type inference fails with nested invocations. If you set either one of the arguments to null, the code compiles.
https://bugs.openjdk.java.net/browse/JDK-8055963
A fix was committed for Java 9, but they also backported it to 8u60:
https://bugs.openjdk.java.net/browse/JDK-8081020
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With