Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Javac cannot infer type from lambda body but ECJ does
Javac cannot infer type from lambda body but ECJ does [message #1754309] Thu, 16 February 2017 09:54 Go to next message
Jens Auer is currently offline Jens AuerFriend
Messages: 11
Registered: February 2017
Junior Member
I ran into a problem where Java code using generics with lambda functions compiles in Eclipse but not with ECJ. The following code compiles fine with ECJ:
import java.util.function.Function;

public class Playbook {

    static class A {
    }

    static class B {
    }

    static class Property<T> {
    }

    static class Var<T> {
        static <T, U> Var<U> mapBidirectional(final Property<T> src, final Function<? super T, ? extends U> f, final Function<? super U, ? extends T> g) {
            return null;
        }

        void bindBidirectional(final Property<T> other) {

        }
    }

    private static A f(final B x) {
        return null;
    }

    private static B g(final A x) {
        return null;
    }

    public void bindTimeString1(final Property<A> timepoint, final Property<B> textProperty) {
        Var.mapBidirectional(textProperty, s -> new A(), t -> new B()).bindBidirectional(timepoint);
    }

    public void bindTimeString2(final Property<A> timepoint, final Property<B> textProperty) {
        Var.mapBidirectional(textProperty, Playbook::f, Playbook::g).bindBidirectional(timepoint);
    }

    void f(final Property<Integer> p) {

    }
}


But javac rejects it with an error:
[ERROR]Playbook.java:[35,90] incompatible types: main.java.Playbook.Property<main.java.Playbook.A> cannot be converted to main.java.Playbook.Property<java.lang.Object>


From reading on Stackoverflow, it could be a limitation/bug in javac, but I am not sure since other answers indicate that this is mandated by the JLS. IMHO this code should compile with javac since the types can be inferred correctly, as proven by ECJ.
Re: Javac cannot infer type from lambda body but ECJ does [message #1754354 is a reply to message #1754309] Thu, 16 February 2017 17:00 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Welcome to Java 8 type checking Smile
Lot's of corner cases exist, where the three entities, JLS, javac and ecj have four different opinions Smile
Luckily, for your example I can confirm that Oracle seems to have fixed a bug to the result that your example will be accepted in Java 9 (tried with build 9-ea+152).
Re: Javac cannot infer type from lambda body but ECJ does [message #1754426 is a reply to message #1754354] Fri, 17 February 2017 16:25 Go to previous messageGo to next message
Jens Auer is currently offline Jens AuerFriend
Messages: 11
Registered: February 2017
Junior Member
Thanks for testing this.

It's amazing how many bugs are found in the type inference of Java 8. On top of that there is at one issue in the type system itself which allows you to compile unsound code without casts which generates a type-error at runtime. Example code and the paper can be found at http://io.livecode.ch/learn/namin/unsound.
Re: Javac cannot infer type from lambda body but ECJ does [message #1754458 is a reply to message #1754426] Sat, 18 February 2017 00:07 Go to previous message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Type inference in a language like Java is inherently complex. The authors of JLS did a tremendous job, still not good enough to ensure that different compilers *always* come to the same result. We're still working on this Smile

Regarding the paper by Amin & Tate: I like to disagree with one of their examples. See https://bugs.eclipse.org/510900 for analysis why JLS 8, ecj and javac 9 all reject the unsound program as expected.
Previous Topic:Error message: Target runtime jdk1.8.0.121 is not defined
Next Topic:JRE library unapears
Goto Forum:
  


Current Time: Wed Oct 09 19:35:04 GMT 2024

Powered by FUDForum. Page generated in 0.03769 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top