Execute Future.sequence with custom ExecutionContext

血红的双手。 提交于 2020-01-02 03:46:25

问题


I'm trying to create a Future[List[Int]] from a List[Future[Int]] using a specified ExecutionContext. However, I'm getting errors about a second implicit parameter cbf of type CanBuildFrom. I don't fully understand the purpose of the CanBuildFrom parameter, and I'm getting errors when I try to omit it that look like the following:

- not enough arguments for method sequence: (implicit cbf:       scala.collection.generic.CanBuildFrom[List[scala.concurrent.Future[Int]],Int,List[Int]]

Can someone explain this, and suggest a solution? Here is my current test code, which suffers the above compilation error:

val my: List[Future[Int]] = Future.successful(1) :: Future.successful(2) :: Future.successful(3) :: Nil
val zz: Future[List[Int]] = Future.sequence(my)(ec)

回答1:


Future.sequence needs a CanBuildFrom to build the collection inside the Future it returns. Many other methods in the standard library require a CanBuildFrom, for example most map methods in the collections API.

Future.sequence's implicit parameter list consists of two parameters, and both must be present in any invocation. To specify one explicitly and the other implicitly, use implicitly. For example:

val zz: Future[List[Int]] = Future.sequence(my)(implicitly, ec)


来源:https://stackoverflow.com/questions/23796942/execute-future-sequence-with-custom-executioncontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!