Reputation: 2607
I have the following test method
def test() = {
val aa = Future{10} map {
case x if x%2 == 0 => (x,x)
case _ => val kk = Future.failed(BadRequestException("dd")); kk
}
aa
}
If I remove the case _
then the return type of method is Future[(Int, Int)]
which is exactly what I want. However, when I add the case _
scenario the return type becomes Future[Object]
. I am not able to understand what should I change as other similar methods in our code base do similar things(I am sure I am missing something subtle) and the return type remains of the nature Future[(Int, Int)]
. Afterall, when I am throwing an exception for a scenario, I would not want the return type of the method to be changed. What am I missing or doing wrong?
Upvotes: 1
Views: 466
Reputation: 11587
you should use flatMap
instead of map
as shown below. with map
method the return type of case _
is Future[BadRequestException]
and for case x if x%2 == 0
the return type is (Int, Int)
. This is leading you to have a net return type of Future[Object]
. This can be corrected by using the flatMap instead of map as shown below.
def test() = Future{10} flatMap {
case x if x%2 == 0 => Future.successful((x,x))
case _ => Future.failed(BadRequestException("dd"))
}
Upvotes: 3