Mapper factory, overloaded handlers and eraser - no warning from the compiler

Boaz Nahum boaznahum at gmail.com
Sat Dec 15 16:41:43 PST 2012


In Fuctions {

  private static final Function<Object, String> STRING = String::valueOf;

   public static <T> Function<T, String> string() {
        return (Function<T, String>) STRING;
   }

}


But because of valueOf  is overloaded there is no way that the 'correct'
method is called, for example


        char[] data = new char[] {'h', 'e', 'l', 'l', 'o'};
        Function<char[], String> vo = Functions.string();
        System.out.println( vo.apply(data));

Produces:
      [C at 11da1f99

But:
        Function<char[], String> vo = String::valueOf;
        System.out.println( vo.apply(data));

Produces 'hello'

Is just a matter of documentation ?
         /**
           * Returns a mapper which performs a mapping from {@code <T>} to
it's
           * string representation.
         */

And the 'string representation' of char[] is char[].toString() ?


----------------------
More important:
----------------------

static <T> Function<T, String> string() {
        return String::valueOf;
 }


The compiler DOES NOT warn you that due to the eraser(?)  valueOf(Object)
is always called !!



Thanks
Boaz


More information about the lambda-dev mailing list