play / scala,隐含请求=>是什么意思?

前端之家收集整理的这篇文章主要介绍了play / scala,隐含请求=>是什么意思?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Implicit parameter for literal function                                    1个
大多数播放框架我都看到了代码

// Returns a tasks or an 'ItemNotFound' error
def info(id: Long) = SecuredApiAction { implicit request =>
  maybeItem(Task.findById(id))
}

是的,我的理解是定义一个方法信息(id:Long),并在scala doc中创建scala中的函数,语法如下:

def functionName ([list of parameters]) : [return type] = {
   function body
   return [expr]
}

你能告诉我隐含的请求=>是什么意思吗?和SecuredApiAction放在{之前

解决方法

play.api.mvc.Action具有处理请求和返回结果的辅助方法. One if it’s apply overloads accepts a play.api.mvc.Request parameter

def apply(request: Request[A]): Future[Result]

通过将请求参数标记为隐式,您允许隐式要求参数的其他方法使用它.它还在Play Framework documentation中说:

It is often useful to mark the request parameter as implicit so it can
be implicitly used by other APIs that need it.

如果您自己创建了一个方法,并且如果它的参数是隐式的,则标记为:

object X {
  def m(): Unit = {
    implicit val myInt = 42
    y()
  }

  def y()(implicit i: Int): Unit = {
    println(i)
  }
}

因为范围中存在隐式,所以在调用y()时,myInt变量将隐式传递给方法.

scala> :pa
// Entering paste mode (ctrl-D to finish)

object X {
  def m(): Unit = {
    implicit val myInt = 42
    y()
  }

  def y()(implicit i: Int): Unit = {
    println(i)
  }
}

// Exiting paste mode,now interpreting.

defined object X

scala> X.m()
42

猜你在找的Scala相关文章