You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importscala.concurrent._importscala.concurrent.ExecutionContext.Implicits.globalfor {
a <-Future { "a" }
b <-Future { "b" }
} yield { println(a + b) }
and warn Missed concurrency opportunity. Future b waits for Future a but may not need to
Conditions:
For yield
Top level Monad is scala.concurrent.Future[_]
Future { "b" } doesn't refer to variable A
Details:
A for-yield statement is a series of flatMaps with a map at the end, therefore the code above waits for a to return successfully before starting b. By rearranging the for-loop you can theoretically speed up the application. For example this would be faster assuming the Futures were slower:
Future.sequence(Seq(Future { "a" }, Future { "b" })).map {
case a :: b ::Nil=> println(a + b)
case _ =>
}
It's just an idea.
The text was updated successfully, but these errors were encountered:
philliptaylorpro
changed the title
Enhancement: for {} with Future Monad is a common concurrency bug
Enhancement: for-yield with Future Monad is a common concurrency bug
Mar 6, 2020
It would be good if Scapegoat could see this:
and warn Missed concurrency opportunity. Future b waits for Future a but may not need to
Conditions:
Details:
A
for-yield
statement is a series of flatMaps with a map at the end, therefore the code above waits for a to return successfully before starting b. By rearranging the for-loop you can theoretically speed up the application. For example this would be faster assuming the Futures were slower:It's just an idea.
The text was updated successfully, but these errors were encountered: