加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 18:06:24 所属栏目:安全 来源:网络整理
导读:参见英文答案 Implicit parameter for literal function????????????????????????????????????1个 大多数播放框架我都看到了代码块 // Returns a tasks or an 'ItemNotFound' errordef info(id: Long) = SecuredApiAction { implicit request = maybeItem(Ta
参见英文答案 > 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读