Advertisement
Guest User

Scala wrapper basics confusion

a guest
Sep 19th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.80 KB | None | 0 0
  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. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement