Sergey Alaev Sergey Alaev - 27 days ago 27
Scala Question

Scala Future design questions

I do not understand why Scala's

Future
is harder to use and has less functionality than Twitters, while the first has been created after the second. So, here are questions about
scala.concurrent.Future
:


  • Why does
    Future.onComplete
    return
    Unit
    instead of
    Future
    ? Composable methods are much more convenient.

  • Why does Scala's
    Future
    not support cancellation?


Answer

Future.onComplete returns a unit, so the invocations cannot be chained. According to the scala docs, "note that this design is intentional, to avoid suggesting that chained invocations may imply an ordering on the execution of the registered callbacks (callbacks registered on the same future are unordered)."

To compose the future, use combinators like flatMap, andThen, and filter, which all return a Future.

Check out http://docs.scala-lang.org/overviews/core/futures.html#functional-composition-and-for-comprehensions for more details

I think cancellation is quite nuanced. See this discussion for more details: How to cancel Future in Scala?