Andrew
Andrew

Reputation: 6394

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.

Upvotes: 4

Views: 143

Answers (1)

Kim Stebel
Kim Stebel

Reputation: 42047

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)

Upvotes: 8

Related Questions