I want to use Cats EitherT
and OptionT
to handle the type Future[Either[Error, Option[T]]
. Suppose the following methods:
def findTeacher(id: Int): Future[Either[String, Option[Teacher]]]
def findSchool(teacher: Teacher): Future[Either[String, Option[School]]]
Now if I want to call them subsequently in a for-comprehension I can use EitherT
and OptionT
like this:
def getSchoolByTeacherId(id: Int): Future[Either[String, Option[School]]] = {
val result = for {
maybeTeacher <- EitherT(findTeacher(id))
schoolF = maybeTeacher.map(findSchool).getOrElse(Future.successful(Right(None)))
school <- EitherT(schoolF)
} yield {
school
}
result.value
}
I wonder if it's possible to make it more concise maybe by combining OptionT
with EitherT
?
OptionT
too (which you do not use in your example)? – Aloise