Cannot resolve overloaded method using shared lambda

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 

Is JSL compliant, why can't the compiler allow overloaded methods? Was this a fixed bug in jdk8u45?

+7
java java-8
source share
2 answers

This is similar to a bug recently introduced in javac .

The argument in the call to f(..) explicitly of type String[] . This can be shown, for example, by resolving the same expression as a stand-alone expression:

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

These accepted by all versions of compilers.

Clearly, f(Integer[]) not applicable with an argument of type String[] . I don’t see the reasons why the external output outputs the internal resolution to a worse result (for example, Object[] ) than the resolution as a separate expression.

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

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

Therefore, f(Integer[]) clearly does not correspond to the calculation.

+1
source share

You get the pain of a known bug in jvm http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8029718 Do not know how to associate the "fixed version" with the assemblies available on the Oracle website. But in any case, you should work in the latest version and report your results in this error. Perhaps they fixed it, and now there is a regression.

+1
source share

All Articles