Returning preferences I have a function that returns some preferences, but first I have to do some checks to see whether the user is authorised to read them. I'm still not happy with the result (the match is a bit ugly).

Note: Result is a framework class, it just means some kind of response.

// implementations are not relevant.
def canAccessPreferences(id: Long): Future[Boolean] = ???
def doGetPreferences(id: Long): Future[Result] = ???

def getPreferences(id: Long) = Future[Result] {

  canAccessPreferences(id).flatMap {
     case false =&gt;
       Future.successful(Forbidden)
     case true =&gt;
       doGetPreferences(id)
  }
}
The only alternative is using a for comprehension:

for {
  hasAccess &lt;- canAccessPreferences(id)
  result &lt;- if (hasAccess)
    Future.successful(Forbidden)
  else
    doGetPreferences(id)

} yield result


Unfortunately you cannot use the if guard of the for because then you would get an exception if hasAccess is false. And since you're composing Future monads there's no way around the Future.successful either.