I have a situation in a pet Scala project that I don't really know how to overcome.
The following example shows my problem.
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
case class MyBoard(id: Option[Int], name: String)
case class MyList(id: Option[Int], name: String, boardId: Option[Int] = None)
case class ErrorCreatingList(error: String)
def createList(myList: MyList): Future[Either[ErrorCreatingList, MyList]] =
Future {
// Let's close our eyes and pretend I'm calling a service to create this list
Right(myList)
}
def createLists(myLists: List[MyList],
myBoard: MyBoard): Future[Either[ErrorCreatingList, List[MyList]]] = {
val listsWithId: List[Future[scala.Either[ErrorCreatingList, MyList]]] =
myLists.map { myList =>
createList(myList.copy(boardId = myBoard.id))
}
// Meh, return type doesn't match
???
}
I wanted createLists
to return Future[Either[ErrorCreatingList, List[MyList]]]
but I don't know how to do it, because listsWithId
has the type List[Future[scala.Either[ErrorCreatingList, MyList]]]
, which makes sense.
Is there a way to do it? A friend told me "and that's what Cats is for", but is it the only option, I mean, can't I do it using just what's in the Scala core library?
Thanks.