文字函数的隐含参数(Implicit parameter for literal function)

编程入门 行业动态 更新时间:2024-10-16 16:05:52
文字函数的隐含参数(Implicit parameter for literal function)

在阅读Play时! 框架文档,我遇到了这个片段:

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

文档解释implicit在那里:

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

此外,我读了这篇文章: Literal with Implicit ,从逻辑上看,函数不能有隐含参数。

如果我很清楚,这是因为一个函数 ,与方法相反总是一个契约(接口)。

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

因此,前一个片段代码的行为是隐式的,因为第一个Action的必需参数是一个文字函数。

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

def apply(block: Request[AnyContent] => Result): Action[AnyContent] def apply(block: => Result): Action[AnyContent] (重定向到第一个)

由于第二个不需要一些参数,是否在存在文字函数的隐式参数时选择了这个参数?

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

Documentation explains implicit there:

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

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 ...

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

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] (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将被传递给函数 session以访问字段 myImplicitClass.session ,允许您省略字段访问。 这正是Play的伎俩! 框架正在使用,检查Controller以查找session功能。

作为旁注,上面的闭包并没有声明它需要一个隐式参数,它是一种语言功能,以避免必须执行以下操作:

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

当一个人想要在闭包体中使用闭包参数作为隐式值时。 另请注意,从Scala 2.9开始,您只能使用具有1个参数的闭包。

Consider the following code:

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

If this function:

def session(implicit myImplicitClass: MyImplicitClass): Int = myImplicitClass.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 }

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.

更多推荐

本文发布于:2023-08-03 10:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1387025.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   参数   文字   Implicit   literal

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!