Mandroid
Mandroid

Reputation: 7524

Why foreach instead of onSuccess for callbacks in Scala?

Scala has foreach construct to install callback on a Future:

myFuture foreach{
 //computation
}

Initially there was onSuccess for same purpose.

onSuccess seems to be a better logical name(do this when future is complete). Why this name 'foreach' then? Does it represent something in context of callbacks which onSuccess is missing?

Upvotes: 0

Views: 285

Answers (1)

Andrey Tyukin
Andrey Tyukin

Reputation: 44992

The only difference between foreach and onComplete seems to be that foreach requires total functions as argument.

The onComplete was deprecated in 2.12.0. The named reasons were:

  1. There were too many callback-methods that were essentially doing the same. In order to avoid confusion, some of them were deprecated, so it is easier to decide when to use what.
  2. Promote for-comprehensions

Link: Deprecations: onSuccess and onFailure.

I'm not sure what to think about this argumentation. For example, there are quite a few redundant methods on Option, couldn't we just remove all but one, and retain only fold?

Upvotes: 2

Related Questions