¿Una referencia de método en Java 8 tiene un tipo concreto y, de ser así, cuál es? [duplicar]
Esta pregunta ya tiene una respuesta aquí:
¿Cómo ejecutar indirectamente una referencia de método en Java 8? 2 respuestasEsta pregunta está muy relacionada conotro. Sin embargo, creo que la respuesta aceptada a esa pregunta no es tan definitiva.
Y quées ¿El tipo de referencia de método en Java 8? Aquí hay una pequeña demostración de cómo una referencia de método se puede "convertir" (¿levantar?) En unjava.util.function.Function
:
package java8.lambda;
import java.util.function.Function;
public class Question {
public static final class Greeter {
private final String salutation;
public Greeter(final String salutation) {
this.salutation = salutation;
}
public String makeGreetingFor(final String name) {
return String.format("%s, %s!", salutation, name);
}
}
public static void main(String[] args) {
final Greeter helloGreeter = new Greeter("Hello");
identity(helloGreeter::makeGreetingFor)
.andThen(g -> "<<<" + g + ">>>")
.apply("Joe");
//Compilation error: Object is not a function interface
// Function
// .identity()
// .apply(helloGreeter::makeGreetingFor)
// .andThen(g -> "<<<" + g + ">>>")
// .apply("Joe");
Function
.<Function<String,String>>identity()
.apply(helloGreeter::makeGreetingFor)
.andThen(g -> "<<<" + g + ">>>")
.apply("Joe");
//Compilation error: Cannot resolve method 'andThen(<lambda expression>)'
// (helloGreeter::makeGreetingFor)
// .andThen(g -> "<<<" + g + ">>>")
// .apply("Joe");
// java.lang.invoke.LambdaMetafactory ???
}
private static <I,O> Function<I,O> identity(final Function<I,O> fun1) {
return fun1;
}
}
Entonces, ¿hay una forma menos dolorosa (más directa) de convertir una referencia de método en un tipo compilado / concreto que se pueda transmitir?