Given these three methods that return Future[Either[String,Int]]:
def greater(x:Int): Future[Either[String,Int]] = Future {
if (x>0)
Right(x)
else
Left("Is not greater")
}
def less(x:Int): Future[Either[String,Int]] = Future {
if (x<0)
Right(x)
else
Left("Is not less")
}
def zero(x:Int): Future[Either[String,Int]] = Future {
if (x==0)
Right(x)
else
Left("Is not zero")
}
The following method that invokes the three above throws a compilation error:
def evaluate(x:Int): Future[Either[String,Int]] = {
val future = greater(x)
future.flatMap { either =>
either.right.flatMap { response => // <-- error in this line
val future2 = less(response)
future2.map { either2 =>
either2.right.map { response2 => zero(response2) }
}
}
}
}
The error:
type mismatch; found : scala.util.Either[String,Nothing] required: scala.concurrent.Future[Either[String,Int]]
How to fix the evaluate method?
evaluatesupposed to return after the future is done? For-1is will beLeft("Is not greater"), doesn't look like something useful. Or was it supposed to be another way round? - laughedelic