4 ms·
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
by sedro 10y ago
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