java - Why does method reference needs a type casting to a Function interface? -


in code snippet ide (idea intellij community) suggesting cast method reference function interface.

why ?

 public class methodreferences {      public static void main(string[] strings) {         function<string, integer> f = integer::parseint;          predicate<person> personpredicate = p->convert(p.getage(),integer::parseint) >10;          //this fine          system.out.println(convert("123", f));          //this fine         system.out.println(convert("123", (function<string, integer>) integer::parseint));          //this not         system.out.println(convert("123", integer::parseint)); //x1     }      private static <t, s> t convert(s s, function<s, t> stfunction) {         return stfunction.apply(s);     } } 

the error message in line x1 is:

error:(20, 19) java: reference println ambiguous   both method println(char[]) in java.io.printstream , method println(java.lang.string) in java.io.printstream match  error:(20, 35) java: incompatible types: inferred type not conform upper bound(s)     inferred: java.lang.integer     upper bound(s): char[],java.lang.object 

it's not accepted because parseint method has 2 different types.

one of them accept 1 string argument, other 1 accept 1 string , 1 int type:

static int parseint(string s)

static int parseint(string s, int radix)

in code, using second one, should cast that. in eclipse, know, ide automatically understand belongs; in intellij, should type casting secure , programming.


Comments