Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala parameter type explanation

I am pretty new to Scala and Play Framework and I never saw the following parameter type before.

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

What is confusing me is the f: => part. If that => weren't there I would think of it as a function that maps a String to a Request and then to a Result.

like image 582
Andrew Avatar asked Feb 19 '23 18:02

Andrew


1 Answers

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

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)
like image 106
Kim Stebel Avatar answered Mar 04 '23 13:03

Kim Stebel