Scala parameter type description

I am new to Scala and the Play Framework, and I have never seen the next type of parameter before.

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

What bothers me is the f: => . If this => not there, I would think of it as a function that maps a String to Request , and then to Result .

+4
source share
1 answer

In the general case, => A is a parameter of a name of type A. This means that the parameter will be evaluated only when and when (and each time) it is used in the body of the function. Thus, f is a parameter of a name whose type is a function that takes a String and returns a function from Request[AnyContent] to a Result . The following is an example of how to evaluate by the name parameter:

 scala> def twice[A](a: =>A) = (a,a) twice: [A](a: => A)(A, A) scala> var i = 0 i: Int = 0 scala> twice { | i += 1 | i | } res0: (Int, Int) = (1,2) 
+8
source

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


All Articles