Let's say we have a class and an overloaded function:
public class Main {
static final class A {
}
public static String g(ToIntFunction<? extends A> f) {
return null;
}
public static String g(ToDoubleFunction<? extends A> f) {
return null;
}
}
and I want to call g with a method reference to a function of type A → int:
public class Main {
static final class A {
}
public static String g(ToIntFunction<? extends A> f) {
return null;
}
public static String g(ToDoubleFunction<? extends A> f) {
return null;
}
private static int toInt(A x) {
return 2;
}
public static void main(String[] args) {
ToIntFunction<? extends A> f1 = Main::toInt;
ToDoubleFunction<? extends A> f2 = Main::toInt;
g(Main::toInt);
}
}
This works with javac, but not with eclipse ecj. I sent an error report to ecj, but I'm not sure if it is an ecj or javac error, and tried to execute an overload resolution algorithm to figure this out. I feel that the code should be accepted, because it is intuitively reasonable, which is ToIntFunctionbetter for toIntthan ToDoubleFunction. However, my reading of JLS is that it should be rejected because there is no excuse for one to be more specific.
, JLS . Main::double2int, 15.13.2. . , , :
, T, T (§9.8), , T.
ToIntFunction<A> ToDoubleFunction<A>. toInt int, , , , ToIntFunction<? extends A> ToDoubleFunction<? extends A> . , ToIntFunction<? extends A> ToDoubleFunction<? extends A>, .
15.12.2.5. , , ToIntFunction ToDoubleFunction Main::toInt A -> int.
S , T e, T S, ( U1... Uk R1 - S, V1... Vk R2 - T):
...
e - (§15.13.1), i) (1 ≤ ≤ k), Ui Vi ii) :
R2 .
R1 <: R2.
R1 , R2 , , .
R1 , R2 , , .
, , , R1 R2 .
ToIntFunction ToDoubleFunction , double int. "R1 <: R2" 4.10.1 . double int , , .
, .
, , , . javac , , . , JLS.