简体   繁体   English

使用自定义ExecutionContext执行Future.sequence

[英]Execute Future.sequence with custom ExecutionContext

I'm trying to create a Future[List[Int]] from a List[Future[Int]] using a specified ExecutionContext . 我正在尝试使用指定的ExecutionContextList[Future[Int]]创建Future[List[Int]] However, I'm getting errors about a second implicit parameter cbf of type CanBuildFrom . 但是,我收到有关CanBuildFrom类型的第二个隐式参数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: 我不完全理解CanBuildFrom参数的用途,当我尝试省略它时出现错误,如下所示:

- 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)

Future.sequence needs a CanBuildFrom to build the collection inside the Future it returns. Future.sequence需要一个CanBuildFrom来在它返回的Future构建集合。 Many other methods in the standard library require a CanBuildFrom , for example most map methods in the collections API. 标准库中的许多其他方法都需要CanBuildFrom ,例如集合API中的大多数map方法。

Future.sequence 's implicit parameter list consists of two parameters, and both must be present in any invocation. Future.sequence的隐式参数列表由两个参数组成,两者都必须存在于任何调用中。 To specify one explicitly and the other implicitly, use implicitly . 要指定一个明确的和隐含等,用implicitly For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM