3

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?
Sergey Alaev
  • 3,851
  • 2
  • 20
  • 35

1 Answers1

6

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?

Community
  • 1
  • 1
Sohan Jain
  • 2,318
  • 1
  • 16
  • 17
  • 1
    map is transformation and I do not want a transformation. I want to add `onSuccess` and `onFailure` handlers on returned Future – Sergey Alaev Feb 27 '16 at 06:17
  • 2
    Ah sorry, good point -- I misunderstood. I updated my answer accordingly. Sounds like scala did not want the `onComplete`, `onSuccess`, and `onFailure` methods to be chainable, because that could imply an order on all chained callbacks. Sounds like a reasonable tradeoff – Sohan Jain Feb 27 '16 at 06:39
  • http://stackoverflow.com/questions/32987855/what-are-advantages-of-a-twitter-future-over-a-scala-future has a few more infos about the difference between the two. – Shastick Nov 10 '16 at 12:27