如何在Scala中解释=>的序列?(How do I interprete a sequence of => in Scala?)

编程入门 行业动态 更新时间:2024-10-17 19:24:06
如何在Scala中解释=>的序列?(How do I interprete a sequence of => in Scala?)

我目前正在开始使用play框架,但我的Scala知识还不够。

据我所知=>表示IsAuthenticated有一些函数作为参数。 我发现f:=> String ...是一个没有输入值的函数。 但是如何用3 =>来解释整行? 再往下,第二行中究竟发生了什么=> f(用户)(请求)? 用户和请求对象的目标函数是什么?

def IsAuthenticated(f: => String => Request[AnyContent] => Result) = Security.Authenticated(username, onUnauthorized) { user => Action(request => f(user)(request)) }

I'm currently starting with play framework, but my Scala knowledge is not sufficient.

As I know the => indicates that IsAuthenticated has some kind of functions as parameter. I found out as well the f: => String... is a function with no input value. But how do I interprete the complete line with its 3 => ? And further down, what exactly happens in the second line with => f(user)(request)? What is the target function for user and request object?

def IsAuthenticated(f: => String => Request[AnyContent] => Result) = Security.Authenticated(username, onUnauthorized) { user => Action(request => f(user)(request)) }

最满意答案

=> String => Request[AnyContent] => Result

添加了parens更容易阅读:

=> (String => (Request[AnyContent] => Result))

你可以在REPL中试试这个。 例如:

scala> def foo(f: => String => Int => Char): Char = f("abcdefgh")(4) foo: (f: => String => (Int => Char))Char

在这个例子中, f是一个nullary函数 call-by-name参数,它返回一个函数(让我们调用那个函数g )。 g是一个接受String参数并返回另一个函数( h )的函数。 h是一个接受Int参数并返回Char的函数。

示例调用:

scala> foo { s: String => { i: Int => s.charAt(i) } } res0: Char = e

让我们在评估时遍历方法体中每个表达式的类型:

f 类型: String => (Int => Char) 值: { s: String => { i: Int => s.charAt(i) } } f("abcdefgh") 类型: Int => Char 值: { i: Int => "abcdefgh".charAt(i) } f("abcdefgh")(4) 类型: Char 价值: 'e' => String => Request[AnyContent] => Result

is easier to read with parens added:

=> (String => (Request[AnyContent] => Result))

You can try this in the REPL. For example:

scala> def foo(f: => String => Int => Char): Char = f("abcdefgh")(4) foo: (f: => String => (Int => Char))Char

In this example, f is a nullary function call-by-name parameter that returns a function (let's call that function g). g is a function that accepts a String parameter and returns yet another function (h). h is a function that accepts an Int parameter and returns a Char.

Example invocation:

scala> foo { s: String => { i: Int => s.charAt(i) } } res0: Char = e

Let's walk through the type of each expression in the method body as it's evaluated:

f Type: String => (Int => Char) Value: { s: String => { i: Int => s.charAt(i) } } f("abcdefgh") Type: Int => Char Value: { i: Int => "abcdefgh".charAt(i) } f("abcdefgh")(4) Type: Char Value: 'e'

更多推荐

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

发布评论

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

>www.elefans.com

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