Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Future.sequence with custom ExecutionContext

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)
like image 209
jonderry Avatar asked May 22 '14 02:05

jonderry


1 Answers

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)
like image 55
wingedsubmariner Avatar answered Nov 12 '22 19:11

wingedsubmariner