是否可以在Scala中使用Java 8样式方法引用? [英] Is it possible to use a Java 8 style method references in Scala?

查看:164
本文介绍了是否可以在Scala中使用Java 8样式方法引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Scala中的JavaFX8应用程序,但我无法弄清楚如何将方法引用传递给事件处理程序。为了澄清,我没有使用ScalaFX库,而是直接在JavaFX上构建我的应用程序。

I'm developing a JavaFX8 application in Scala but I couldn't figure out how to pass a method reference to an event handler. To clarify, I'm not using ScalaFX library but build my application directly on top of JavaFX.

这是相关的代码片段。

InputController.java (我在Java中编写了这个测试类,以隔离问题只使用方法参考)

InputController.java (I wrote this test class in Java to isolate the issue to consume a method reference only)

public class InputController {
    public void handleFileSelection(ActionEvent actionEvent){
        //event handling code
    }

    public InputController() {
        //init controller
    }
}

这适用于(Java)

InputController inputController = new InputController();
fileButton.setOnAction(inputController::handleFileSelection);

这不起作用(Scala)

val inputController = new InputController
fileButton.setOnAction(inputController::handleFileSelection)

这是来自编译器的错误消息(Scala 2.11.6)。

Here's the error message from the compiler (Scala 2.11.6).

Error:(125, 45) missing arguments for method handleFileSelection in class Main;
follow this method with '_' if you want to treat it as a partially applied function
    fileButton.setOnAction(inputController::handleFileSelection)
                                            ^

如果我使用Scala 2.12.0-M2,我会收到不同的错误消息。

If I use Scala 2.12.0-M2 instead, I get a different error message.

Error:(125, 45) missing argument list for method handleFileSelection in class Main
Unapplied methods are only converted to functions when a function type is expected.
You can make this conversion explicit by writing `handleFileSelection _` or `handleFileSelection(_)` instead of `handleFileSelection`.
    fileButton.setOnAction(inputController::handleFileSelection)
                                            ^

是否有Scala可以利用Java 8中引入的方法引用的本机方式?我知道使用lambda表达式的隐式转换方法,但我想知道是否有一种方法可以使用类似于Java 8的方法引用,而无需使用lambda decleration。

Is there a native way which Scala can leverage method references introduced in Java 8? I'm aware of the implicit conversions approach to use a lambda expression but I want to know if there is a way to use a method reference similar to Java 8 without needing to use the lambda decleration.

推荐答案

inputController :: handleFileSelection 是Java语法,Scala不支持或不需要它,因为它已经有了lambdas的简短语法如下: inputController.handleFileSelection _ inputController.handleFileSelection(_) inputController.handleFileSelection 也可以工作,具体取决于上下文。)

inputController::handleFileSelection is Java syntax, which isn't supported or needed in Scala because it already had a short syntax for lambdas like this: inputController.handleFileSelection _ or inputController.handleFileSelection(_) (inputController.handleFileSelection can also work, depending on the context).

但是,在Java中,你可以使用lambdas和方法引用需要SAM(单个抽象方法)接口, EventHandler 就是这样的接口。在版本2.11之前的Scala中根本不允许这样做,在2.11中有使用带有SAM接口的lambdas的实验性支持,必须使用 -Xexperimental scalac标志启用它,从2.12开始,它完全支持,不需要启用。

However, in Java you can use lambdas and method references when any SAM (single abstract method) interface is expected, and EventHandler is just such an interface. In Scala before version 2.11 this isn't allowed at all, in 2.11 there is experimental support for using lambdas with SAM interfaces, which has to be enabled using -Xexperimental scalac flag, and starting from 2.12 it is fully supported and doesn't need to be enabled.

这篇关于是否可以在Scala中使用Java 8样式方法引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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