News for hackers. Stuff that matters.
Re:Scala vs Go by sedro on Tuesday February 14, 2017 @11:37AM (#13644761)
You don't need to specify a return type in scala. In this case the return type is inferred as Option[Long].

You are correct about the exception if the cookie's value can't be parsed as a Long. You might instead write the code as:

  def getUserId()(implicit request: RequestHeader): Option[Long] = {
    request.cookies.get("uid")
      .flatMap(cookie => Try(cookie.value.toLong).toOption)
      .filter(_ > 0)
  }
or, equivalently:

  def getUserId()(implicit request: RequestHeader): Option[Long] = for {
    cookie <- request.cookies.get("uid")
    value <- Try(cookie.value.toLong).toOption if value > 0
  } yield value

There are no comments on this comment yet.