Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java type inference differences between javac 1.8.0_45 and javac 1.8.0_92?

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?

like image 574
sdgfsdh Avatar asked Jun 11 '17 08:06

sdgfsdh


1 Answers

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

like image 170
Luciano van der Veekens Avatar answered Oct 14 '22 17:10

Luciano van der Veekens