How to interpret sequence => in Scala?

I am currently starting with the gaming environment, but my knowledge of Scala is not enough.

As I know, => indicates that IsAuthenticated has some functions as a parameter. I also found out that f: => String ... is a function without an input value. But how do I interpret a complete line with its 3 =>? And further down, what exactly happens on the second line with => f (user) (request)? What is the objective function for the user object and request?

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

easier to read with the addition of partners:

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

You can try this in REPL. For instance:

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

In this example, f is the nullary function parameter to be called by name, which returns the function (let's call this function g). g is a function that takes a String parameter and returns another function (h). h is a function that takes an Int parameter and returns Char.

Call example:

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

Pass through each expression in the body of the method, as it appreciated:

  • 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'
+5
source

A few additions to the accepted answer

  • About Security.Authenticated

    Security.Authenticated (username, onUnauthorized) {user => Action (request => f (user) (request))}

    Security.Authenticated - seems to be a function that accepts two parameter lists. First parameter list:

     (username:String, onUnauthorized:Boolean) 

    and the second list is one argument:

     (g:User => Action) 

    A full signature might be something like this:

     def Authenticated(username:String, onUnauthorized:Boolean)(g:User => Action) 
  • Action is represented by a class that takes a function in it:

     class Action(h:Request => Result) 

    The constructor argument is a new function.

     def hActual(request:Request):Result = f(user)(request) 

    ( def must have user in scope because it is not specified in the arguments. For example, hActual can be declared immediately before calling the Action constructor:

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

    )

  • It also seems that the constructor can be called using curring:

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

Source: https://habr.com/ru/post/1496446/


All Articles