Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't resolve overloaded method with generic lambda

Tags:

java

java-8

This code:

public static void f(String[] args)  {}
public static void f(Integer[] args)  {}

public static void main(String[] args)
{
    f(Stream.of("xxx").toArray(i -> new String[i]));
}

compiles success with jdk8u45 but jdk8u60 prints the following error:

Error:(17, 9) java: reference to f is ambiguous
  both method f(java.lang.String[]) in type_infer.Test and method f(java.lang.Integer[]) in type_infer.Test match

Does it follow the JSL, Why compiler can not resolve overloaded methods? Was it a fixed bug in jdk8u45?

like image 619
And390 Avatar asked Aug 26 '15 10:08

And390


1 Answers

This looks like a bug recently introduced in javac.

The argument in the call to f(..) is clearly of type String[]. This can, e.g., be shown by resolving the same expression as a standalone expression:

String s1 = Stream.of("xxx").toArray(i -> new String[i])[0];
String[] s2 = Stream.of("xxx").toArray(i -> new String[i]).clone();

These are accepted by all versions of compilers.

Clearly, f(Integer[]) is not applicable with an argument of type String[]. I see no reason why the outer type inference would force the inner resolution to a worse result (e.g., Object[]) than resolution as a standalone expression.

Additional evidence: the following statement is correctly rejected even by those versions of javac affected by this bug:

Integer[] s3 = Stream.of("xxx").toArray(i -> new String[i]);

Hence f(Integer[]) is clearly out of the reckoning.

like image 82
Stephan Herrmann Avatar answered Sep 20 '22 18:09

Stephan Herrmann