Lambda expressions in Scala Future

432 Views Asked by At

I´m using Scala 2.12.2 and Java 8. For a really bizarre situation I need to use the Scala Futures in Java, and I see that using the Lambdas in the future operator it does not work.

@Test
public void test(){
    Future<String> future = new Promise.DefaultPromise();
    future.map(value -> value.toUpperCase())
            .onComplete(value -> System.out.println(value));
}

The error message said:

Future cannot be applied to lambda expression

But after read the documentation of the scala version 2.12 http://www.scala-lang.org/news/2.12.0/

I understood that from now on, lambda expressions from Java8 were compatible with Scala API.

Did I misunderstood something here?

1

There are 1 best solutions below

3
On

This here works fine

scala.concurrent.Future<String> f = scala.concurrent.Future.successful("abc");
scala.concurrent.Future<String> f2 = f.map(s -> s.toUpperCase(), scala.concurrent.ExecutionContext.global());
f2.foreach(s -> {System.out.println(s); return null;}, scala.concurrent.ExecutionContext.global());