SHARE
TWEET

Scala wrapper basics confusion

a guest Sep 19th, 2019 112 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // So, Scala Futures and Option wrappers ...
  2.  
  3. // I'm curious about how to check the value of the Option `maybeFoundIt` while still returning a Result (Ok)
  4.  
  5. someService.findByName(name="Looking for this").map(maybeFoundIt => {
  6.   Ok(Json.toJson(maybeFoundIt))
  7. })
  8.  
  9. // Map works here because we preserve the Future, which we want, so we just sneakily change the innards and return
  10. // but how do I check the return value (Option) and adjust my innard based upon that.
  11. // Matching is how you check Option right?
  12.  
  13. // I know Scala likes pattern matching, but this returns a PartialFunction and we need a Future[Result] ¯\_(ツ)_/¯
  14.  
  15. someService.findByName(name="Looking for this").map(maybeFoundIt => {
  16.   case Some(gotIt) => Ok(Json.toJson(gotIt))
  17.   case None => Ok(Json.toJson("status" -> "Not found"))
  18. })
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top