Why does IDEA warn of the return of the Future No?

The following IDEA code snippet displays a warning that future {None} will be redundant:

 queryString match { case Some(query) => ... // Do async call case None => future { None } } 

Is there a better opportunity to do this?

+4
source share
3 answers

You can create an already established future without creating a closure using Future.successful[T](result: T) , so maybe Future.successful(None) is what you want.

Since futures already distinguish between success and failure regardless of their type parameter, however, you can also signal failure through Future.failed(new Exception("No query string")) , given that your asynchronous call may also omit the wrapper in Some .

+5
source

I do not know if the IDEA warning is useful in this case.

You can potentially silence it by clicking on the match down into the future:

 future { queryString match { case Some(query) => Some(computeResult(query)) case None => None } } 

(or more concisely: future { queryString.map(computeResult(_)) } )

+1
source

I think I decided on my own: I do not need the option encapsulated in my Future, because the Future itself can also fail, and therefore the failure is kind of equal to None.

Now I return only the future and:

 queryString match { case Some(query) => //async Call which returns a Future without Option case None => throw new Exception("Error") } 
+1
source

All Articles