I have a very complex class that gets a generic value, a functional interface and some generic typed subclasses. Now I noticed some strange behaviour related to type inferring. Have a look at this code:
public class Test{
public static class SubClass<F>{
public SubClass(){}
}
@FunctionalInterface
public interface FuncInterface {
void operation(String s);
}
@SafeVarargs
public <T> Test(T obj, FuncInterface fi, SubClass<T>...sc){}
@SafeVarargs
public <T> Test(T obj, SubClass<T>...sc){}
public static void main(String[] args){
Test t = new Test(
42,
(s)->{},
new SubClass<>());
}
}
The line Test t = new Test(...); cannot compile due to following error:
The constructor Test(int, (<no type> s) -> {}, new SubClass<>()) is undefined
Now I found two different possibilities to get this code working:
1) Set explicit type for the functional interface parameter
Test t = new Test(
42,
(String s)->{},
new SubClass<>());
2) or remove the overloaded constructor.
/* public <T> Test(T obj, SubClass<T>...sc){} */
I really don't get the problem of the compiler here and why my solutions work. Can somebody please explain what is going on here.
It's surely a problem of Eclipse Compiler for Java (ECJ) which is used by Eclipse internally. To work-around it add argument type to the lambda:
public static void main(String[] args){
Test t = new Test(
42,
(String s)->{},
new SubClass<>());
}
This way it compiles fine (at least in Eclipse Luna 4.4.2).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.