I try to use a for expression to iterate through a list, and then do the conversion for each item using a utility that returns Future. In short, it does not compile, and I would like to understand why. I read this question , which is similar, and was of great help, but what I'm trying to do is even simpler, which is even more confusing why it does not work. I am trying to do something like:
import scala.concurrent.Future import scala.concurrent.ExecutionContext.Implicits.global val numberList = List(1, 2, 3) def squareInTheFuture(number: Int): Future[Int] = Future { number * number} val allTheSquares = for { number <- numberList square <- squareInTheFuture(number) } yield { square }
And I get:
error: type of discrepancy; found: scala.concurrent.Future [Int] required: scala.collection.GenTraversableOnce [?] square <- squareInTheFuture (number) ^
Can someone help me understand why this is not working and what is the best alternative?
source share