Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala using Java libraries, taking advantage of lambda expressions support in Java 8

According to: http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#lambda-expressions-in-gui-applications

Previously:

btn.setOnAction(new EventHandler<ActionEvent>() {

    @Override
    public void handle(ActionEvent event) {
        System.out.println("Hello World!");
    }
});

Now, we can:

btn.setOnAction(
  event -> System.out.println("Hello World!")
);

Now, I try to do that in Scala when using a Java library.

I'm using JavaFX (which is included by default in Java 1.8 SE). Try:

chart.setOnMouseClicked( (e: MouseEvent) => println("Noice") )

However, I get:

Error:(204, 46) type mismatch;
 found   : javafx.scene.input.MouseEvent => Unit
 required: javafx.event.EventHandler[_ >: javafx.scene.input.MouseEvent]
    chart.setOnMouseClicked( (e: MouseEvent) => println("Noice") )
                                             ^

The old style works fine:

chart.setOnMouseClicked( new EventHandler[MouseEvent] {
  override def handle(event: MouseEvent): Unit = println("NOT NOICE")
} )

I set the project language level to Java 8 in IntelliJ, I'm using Scala 2.11.1, and Java from Oracle version 1.8.0_05

What am I missing here? or is it simply not possible to pass a lambda expression from Scala to Java the same way it is done in the mentioned example?

like image 531
SpaceMonkey Avatar asked Jun 23 '14 15:06

SpaceMonkey


2 Answers

For scala versions 2.12 onward support comes out of the box.

State for pre-2.12:

Lambdas was introduced in java language and has a little in common with scala functions. They're compiled down to a different bytecode, has different hierarchy (scala functions were here long before and apparently java designers have chosen clean room implementation without compatibility with scala).

Currently support is pretty much limited and what you're trying to do is not possible (out of the box):

The Scala 2.11 series targets Java 6, with (evolving) experimental support for Java 8. In 2.11, Java 8 support is mostly limited to reading Java 8 bytecode and parsing Java 8 source. We will be expanding Scala's (experimental) Java 8 support and interop throughout the 2.11 series. - See more at: https://typesafe.com/blog/scala-211-has-arrived#sthash.ukr4FSpU.dpuf

There is ongoing efforts to resolve this problem

See also discussion on scala roadmap to support java 8 functions.

like image 70
om-nom-nom Avatar answered Oct 17 '22 22:10

om-nom-nom


One way to achieve this is using implicit transformations what you need to do is to create a new Object that handles all this transformations, something like this:

import javafx.scene.input.MouseEvent
import javafx.event.EventHandler

object FXEvent2HandlerImplicits{
  implicit def mouseEvent2EventHandler(event:(MouseEvent)=>Unit) = new EventHandler[MouseEvent]{
    override def handle(dEvent:MouseEvent):Unit = event(dEvent)
  }
}

Then just import it in any file that you may need the transformation:

import FXEvent2HandlerImplicits._   
//From now on within scope you can now call events like Java8 Lambdas
chart.setOnMouseClicked( (e: MouseEvent) => println("Noice") )

This is just Syntactic sugar to archive the same thing in a more elegant way

like image 20
Octavio Luna Avatar answered Oct 17 '22 22:10

Octavio Luna