方法参考的组成 [英] Composition of method reference

查看:133
本文介绍了方法参考的组成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与此问题有关:(也适用于方法引用)。这就是为什么您需要转换(或分配)在方法可用之前键入 Function



原因Eclipse显示来自JDK编译器(javac)的不同错误消息是Eclipse使用自己的Java编译器,称为ecj,它与javac完全不同。这就是BTW,为什么Eclipse可以在JRE上运行,而不需要完整的JDK安装。


This is related to this question: How to do function composition?

I noticed that a method reference can be assigned to a variable declared as Function, and so I assume it should have andThen or compose function, and hence I expect that we can compose them directly. But apparently we need to assign them to a variable declared as Function first (or type-cast before invocation) before we can call andThen or compose on them.

I suspect I might have some misconception about how this should work.

So my questions:

  1. Why do we need to type-cast or assign it to a variable first before we can call the andThen method?
  2. What exactly is the type of method reference that it needs to be done in this way?

Sample code below.

public class MyMethods{
    public static Integer triple(Integer a){return 3*a;}
    public static Integer quadruple(Integer a){return 4*a;}

    public int operate(int num, Function<Integer, Integer> f){
        return f.apply(num);
    }

    public static void main(String[] args){
        MyMethods methods = new MyMethods();
        int three = methods.operate(1, MyMethods::triple); // This is fine
        // Error below
        // int twelve = methods.operate(1, (MyMethods::triple).andThen(MyMethods::quadruple));
        // But this one is fine
        Function<Integer, Integer> triple = MyMethods::triple;
        Function<Integer, Integer> quadruple = MyMethods::quadruple;
        int twelve = methods.operate(1, triple.andThen(quadruple));
        // This one is also fine
        int twelve2 = methods.operate(1, ((Function<Integer, Integer>)MyMethods::triple).andThen(MyMethods::quadruple));
    }
}


More description on the error

In Eclipse it's highlighted with the error message:

The target type of this expression must be a functional interface

and in Java 8 compiler the error is:

java8test.java:14: error: method reference not expected here
        int twelve = methods.operate(1, (MyMethods::triple).andThen(MyMethods::quadruple));
                                         ^
1 error

(actually, why is the error in Eclipse different from the one from Java 8 compiler?)

解决方案

As Brian Goetz (project lead for Java lambdas) says, "Lambda expressions have no intrinsic type" (which applies to method references too). This is why you need to cast (or assign) to type Function before its methods become available.

The reason Eclipse shows different error messages from the JDK compiler (javac) is that Eclipse uses its own Java compiler, called ecj, which is a totally different program from javac. This is, BTW, why Eclipse can run on the JRE, without needing a full JDK install.

这篇关于方法参考的组成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆