ALMEK ALMEK - 1 month ago 6
Scala Question

What is the best way of letting Future values, into case class constructer, that doesn't accept futures?

Constructing a case class, that its constructer values coming by Future.
as example:

case class Info(values: Seq[Int], result: Long)
val vs: Future[Seq[Int]] = getValues()
val r: Future[Long] = calcValues(vs)


I want then to construct the Info by something like this:

val info = Info(vs,r)


what is the best way of succeeding the vs and r futures for the constructure

Answer

You can use for comprehension, like below.

val future: Future[Info] = for (
  vs <- getValues();
  r <- calcValues(vs)
) yield Info(vs, r)

This returns a Future[Info] which you can map over and do your other operations.