When should I make methods with implicit argument in Scala?

被刻印的时光 ゝ 提交于 2019-12-19 03:37:14

问题


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?


回答1:


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) }


来源:https://stackoverflow.com/questions/15248022/when-should-i-make-methods-with-implicit-argument-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!