News for hackers. Stuff that matters.
Re:Scala vs Go by etaty on Tuesday February 14, 2017 @10:39AM (#13644234)
The scala example is plain wrong.

import play.api.mvc.RequestHeader

def getUserId()(implicit request: RequestHeader) = { request.cookies.get("uid").map(_.value.toLong).filter(_ > 0) }

Return type is missing, it will throw exception (toLong).

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
Re:Scala vs Go by mark242 on Tuesday February 14, 2017 @11:49AM (#13644872)
It's okay, since cookies is a Map[String, String] so running the get returns an Option[String].

If the author is concerned, the way to express this to be more expressive could be something like this:

    val uidOpt: Option[Long] = request.cookies.get("uid") map {
        case Some(uidStr) if uidStr.toLong > 0 => Some(uidStr.toLong)
        case _ => None
    }
Re:Scala vs Go by jcdavis on Tuesday February 14, 2017 @11:44AM (#13644829)
function return types are not required in scala, although every style guide I've seen requires them for any non-trivial function
Re:Scala vs Go by mi100hael on Tuesday February 14, 2017 @11:48AM (#13644854)
I only use them on methods/vals that are intended to be publicly used just so I don't inadvertently change the type of something and break someone else's code. Within private methods, it doesn't really matter.
Re:Scala vs Go by dcu on Tuesday February 14, 2017 @11:56AM (#13644926)
it's funny, this discussion alone kind of proves the author's point