Javac cannot infer type from lambda body but ECJ does [message #1754309] |
Thu, 16 February 2017 09:54 |
Jens Auer 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.
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03769 seconds