文字函数的隐式参数 [英] Implicit parameter for literal function

查看:96
本文介绍了文字函数的隐式参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

边看边玩!框架文档,我遇到了以下片段:

While reading Play! Framework documentation, I came across this snippet:

def index = Action { implicit request =>
  session.get("connected").map { user =>
    Ok("Hello " + user)
  }.getOrElse {
    Unauthorized("Oops, you are not connected")
  }
}

文档在此处解释implicit:

或者,您可以从请求中隐式检索会话

Alternatively you can retrieve the Session implicitly from a request

此外,我读了这篇文章:文学与内隐,从逻辑上看,函数不能具有隐式参数.

Besides, I read this post: Literal with Implicit and it seems logically that function cannot have implicit parameter.

如果我很清楚,这是因为与方法相反的功能始终具有合同(接口).

If I well figured out, this is because a function, contrary to method has always a contract (interface).

例如,实际上Function1[Int, Function1[Int, Int]]具有Int作为返回类型的第一个参数,因此阻止我们将其注释为implicit.这将导致对其高级返回类型:() => IntInt => Int ...

Indeed, for instance, Function1[Int, Function1[Int, Int]] has as a return type's first parameter an Int, and thus prevents us to annotate this one as implicit. This would lead to a confusion about its high-level return type: () => Int or Int => Int ...

因此,由于第一个Action的required参数是文字函数,因此之前的代码段具有隐式行为.

Therefore, what the previous snippet code behaves with implicit since first Action's required parameter is a literal function.

我想允许编译器接受此代码的原因是Action.apply()方法的多个签名:

I guess the reason allowing compiler to accept this code is the multiple signatures of Action.apply() method:

  • def apply(block: Request[AnyContent] => Result): Action[AnyContent]
  • def apply(block: => Result): Action[AnyContent](重定向到第一个)
  • def apply(block: Request[AnyContent] => Result): Action[AnyContent]
  • def apply(block: => Result): Action[AnyContent] (redirecting to the first one)

由于第二个不需要某些参数,是否在存在文字函数的隐式参数的情况下选择了该参数?

Since the second doesn't need some parameter, is this one selected in presence of a literal function's implicit parameter?

推荐答案

请考虑以下代码:

class MyImplicitClass(val session: Int)
object Tester {
  def apply(fun: MyImplicitClass => Int): Int = ???
  def apply(fun: => Int): Int = ???
}
Tester { implicit myImplicitClass => session * 20}

如果此功能:

def session(implicit myImplicitClass: MyImplicitClass): Int = myImplicitClass.session

在范围内,然后将编译第一个代码段,因为很明显,隐式参数myImplicitClass将传递给 function session以便访问字段 myImplicitClass.session,允许您省略字段访问.这正是玩法的窍门!框架正在使用,请检查 查找session函数.

is in scope, then the first code snippet will compile, because clearly the implicit parameter myImplicitClass will be passed to the function session in order to access the field myImplicitClass.session, allowing you to omit the field access. This is exactly the trick the Play! Framework is using, check Controller to find the session function.

请注意,上述闭包并未说明它采用了隐式参数,这是一种语言功能,可以避免必须执行以下操作:

As a side note, the above closure is not stating that it takes an implicit parameter, it's a language feature to avoid having to do the following:

Tester { myImplicitClass => 
  implicit val x = myImplicitClass
  session * 20
}

当要使用闭包参数作为闭包主体中的隐式值时.另外请注意,从Scala 2.9开始,此技巧仅限于带有正好1个参数的闭包.

when one wants to use a closure parameter as an implicit value in the body of the closure. Also note that as of Scala 2.9, you are limited to closures with exactly 1 parameter for this trick.

这篇关于文字函数的隐式参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆