Java vs Scala Using the functional interface

In Java, I can do this:

Runnable task = () -> { System.out.println("Task is running"); };

But how to come to Scala I can not do the same!

val task: Runnable = () => {println("Task is running")}

I get a compiler error! I am using Scala version 2.11.8.

type mismatch;  found   : () => Unit  required: Runnable
+4
source share
1 answer

Scala version 2.12 supports converting lambda expressions to types using the "single abstract method" (SAM), as well as the " Functional Interface " like Java 8. See http://www.scala-lang.org/news/2.12. 0 # lambda-syntax-for-sam-types .

Scala - Java/ SAM (, Runnable). , 2.12.

, , Scala 2.12.

+11

Source: https://habr.com/ru/post/1675837/


All Articles