Understanding the Scala Function from the zentask Play Platform Example

This is a function in the zentask example in Application.scala. I'm trying to figure it out ...

What does f: => String mean?

How about chaining f: => String => Request [AnyContent] => Result

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

The form parameter fn: => String is a "generator" (either a function or a value) that returns (or is) a String, so for example, you might have a method defined as

 def myMethod(fn: => String): String = "Fn output = " + fn 

and call it this way (the return types that I use here can usually be deduced by the compiler, I just add them for pedagogical purposes):

 def myFn: String = "Hello!" // Alternatively: def myFn(): String = "Hello!" // or: val myFn: () => String = "Hello!" // or most simply: val myString = "Hello!" val output = myMethod(myFn) // output = "Fn output = Hello!" 

Based on this, we can define a method that takes a function that turns String into Int, for example:

 def my2ndMethod(fn: String => Int): Int = fn("4") 

and name it as follows:

 def my2ndFn(input: String) = 5 * input.toInt // Alternatively: val my2ndFn: String => Int = input => 5 * input.toInt val output2 = my2ndMethod(my2ndFn _) // output2 = 20 

In case you provide, you have a more complex entity: that which returns (or is) a function that takes a string and returns an additional function, which in turn accepts Request[AnyContent] and (finally) returns the result ( phew!).

You can also think of it as a function assignment and use it as follows:

 def authFn(username: String)(request: Request[AnyContent]): Result val authenticatedResult = IsAuthenticated(authFn _) 
+3
source

It’s actually a little difficult to understand how the operator is associated:

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

coincides with

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

So f is a function that takes => String and returns a function that takes a request and returns a result. As I pointed out in my commentary, see The required simple English translation of the following scala snippet for an explanation of what is happening.

So, why => String as the first argument against just String ? I assume that it comes into play if you want the user to pass a function that has its first argument by name (which means that it is evaluated every time it is needed).

Let's say you have a g method:

 def g(s: => String): String => String = arg => s + arg 

If you want to write a method m that takes g as an argument, then you need to write it like this:

 def m(f: (=> String) => (String => String)) = f("a")("b") m(g) // compiles 

If you write it like this:

 def n(f: String => (String => String)) = f("a")("b") n(g) // does not compile // found : => String => (String => String) // required: String => (String => String) 
+1
source

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


All Articles