Every story and comment from the Hacker News API, in a layout borrowed from 2001.
A comment on Scala vs Go. Parent comment
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) }
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.
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:
or, equivalently: