18

The introduction of CompletableFutures in Java 8 brought to the language features available in the scala.concurrent.Future such as monadic transformations.

  • What are the differences, and why a Scala developer should prefer Scala Futures over java 8 CompletableFuture ?

  • Are there still good reasons to use the scala.concurrent.Futurein Java through akka.dispatch bridge?

Edmondo
  • 19,559
  • 13
  • 62
  • 115
  • imho, because monadic operations on CompletableFuture isn't defined coherently with monadic operations neither in Java 8 nor in Scala collections - so semantic isn't consistent. And also for me such semantic seems too redundant. + You will have to deal with Java-Scala interoperability without any profit from that. – dk14 Feb 18 '15 at 17:34
  • Can you please detail your comment with am answer? What about also the akka.dispatch bridge to use Scala futures in Java? – Edmondo Feb 18 '15 at 17:49
  • 1) I just mean that there is no `stream` method on CompletableFuture, so you can't just call map-reduce (and as a reult functions like filter) there as usual - have to use some ugly methods. also there is no for-comprehension for CompletableFuture of course. I didn't use CompleteFuture in practice (it's so ugly) - so can't answer your question completely 2) As I understand the topic is about java's futures in scala. – dk14 Feb 18 '15 at 18:26
  • You are right about the for comprehension. I also added a second point, can you please provide your opinion? – Edmondo Feb 19 '15 at 08:26
  • 2
    there is some potential interoperabilty problems: akka.dispatch.Future requires scala's functions. There is an eperimental interoperabilty with Java 8 functions for Scala 2.11/2.12 - https://github.com/scala/scala-java8-compat, but you know it's experimental. – dk14 Feb 19 '15 at 09:38
  • This blogpost might provide some context for Scala futures: http://danielwestheide.com/blog/2013/01/16/the-neophytes-guide-to-scala-part-9-promises-and-futures-in-practice.html – Others Feb 27 '15 at 17:31

1 Answers1

21

What are the differences, and why a Scala developer should prefer Scala Futures over java 8 CompletableFuture ?

Rephrasing what @dk14 pointed out in comments I'd say that CompletableFuture doesn't have idiomatic Scala api.

For scala developer the implications are:

  • lack of for comprehensions due to fact that it does not follow Scala method conventions common for monadic types
  • java-scala interop overhead needed when using big part of it's methods

It is also worth noting that java CompletableFuture is not exactly equivalent of scala Future. It is rather a fuse of scala Future and Promise.

Considering the cons listed above there isn't much sense in using CompletableFuture in scala unless you are designing public api that should be seamlessly interoperable with java.

Are there still good reasons to use the scala.concurrent.Future in Java through akka.dispatch bridge?

I am particularly looking for reasons to use akka.dispatch in Java, if there are still any

Akka is build on top of scala and it sometimes uses scala Futures. This means that in cases when you have some portion of code written in java it is worth to wrap it in scala api (with akka.dispatch java api) to be able to easily use it with akka.

For example, you are implementing akka actor in java. When processing message you want to do some non-blocking reading that, when done, should produce result as a message to another actor.

What you could do is to put your I/O into java Callable, then use akka.dispatch.Futures#future to get scala Future out of it, and then you could leverage akka pipe to make result of the future be delivered as a message to some actor.

Community
  • 1
  • 1
Eugene Loy
  • 12,224
  • 8
  • 53
  • 79