Based on an Eclipse compiler issue: https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1753.
The following code
public class X {
interface Data {}
@SuppressWarnings("unchecked")
public <T extends Data> T getData() {
return (T) new Data() {};
}
public static void assertEquals(Short expected, Short actual) {}
public static void assertEquals(Byte expected, Byte actual) {}
public static void assertEquals(Object expected, Object actual) {}
public void testData() {
assertEquals(getData(), getData());
}
}
gives this compilation error:
reference to assertEquals is ambiguous assertEquals(getData(), getData()); both method assertEquals(Short,Short) in X and method assertEquals(Byte,Byte) in X match
I argue that despite this being excused by the JLS (see linked issue above), the error message is not good. `Short` or `Byte` should never be inferred, as the method returns <T extends Data>. These are 2 separate type hierarchies. Furthermore, the compiler has all the information it needs to resolve correctly to the Object overload method.
The following code
public class X {
interface Data {}
@SuppressWarnings("unchecked")
public <T extends Data> T getData() {
return (T) new Data() {};
}
public static void assertEquals(Short expected, Short actual) {}
public static void assertEquals(Byte expected, Byte actual) {}
public static void assertEquals(Object expected, Object actual) {}
public void testData() {
assertEquals(getData(), getData());
}
}
gives this compilation error:
reference to assertEquals is ambiguous assertEquals(getData(), getData()); both method assertEquals(Short,Short) in X and method assertEquals(Byte,Byte) in X match
I argue that despite this being excused by the JLS (see linked issue above), the error message is not good. `Short` or `Byte` should never be inferred, as the method returns <T extends Data>. These are 2 separate type hierarchies. Furthermore, the compiler has all the information it needs to resolve correctly to the Object overload method.