我目前正在开始使用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] => Resultis 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))CharIn 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 = eLet'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'更多推荐
发布评论