Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I make methods with implicit argument in Scala?

I made codes using play framework in scala which look like the following:

object Application extends Controller {
  def hoge = Action( implicit request =>
    val username = MyCookie.getName.get
    Ok("hello " + username)
  }
}

object MyCookie {
  def getName( implicit request: RequestHeader ) = {
    request.cookies.get("name").map(_.value)
  }
}

I got a code review from my coworker. He said this code is not readable because of implicit parameter. I couldn't reply to his opinion. So could you tell me what is the best way to use implicit parameters? When should I use implicit parameters?

like image 982
buster84 Avatar asked Mar 06 '13 13:03

buster84


1 Answers

You should use implicit parameters when there is almost always a "right" way to do things, and you want to ignore those details almost all the time; or when there often is no way to do things, and the implicits provide the functionality for those things that work.

For an example of the first case, in scala.concurrent.Future, almost every method takes an implicit ExecutionContext. You almost never care what your ExecutionContext is from call to call; you just want it to work. But when you need to change the execution context you can supply it as an explicit parameter.

For an example of the second case, look at the CanBuildFroms in the collections library. You cannot build anything from anything; certain capabilities are supplied, and the lack of an implicit that, say, lets you package up a bunch of Vector[Option[String]]s into a HashSet[Char] is one major way to keep the library powerful and flexible yet sane.

You are doing neither: apparently you're just using it to save a little typing in one spot at the expense of the other spot. And, in this case, in doing so you've made it less obvious how things work, as you have to look all over the place to figure out where that implicit request actually gets used. If you want to save typing, you're much better off using short variable names but being explicit about it:

Action{ req => val name = MyCookie.getName(req).get; Ok("hello "+name) }
like image 78
Rex Kerr Avatar answered Oct 20 '22 00:10

Rex Kerr