Oleg
Oleg

Reputation: 296

Params list in Scala function. Can someone explain the code?

Can someone explain the Scala code used in trait Secured of playframework sample app zentask:

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

I've just started to learn Scala and can not figure out this sequence f: => String => Request[AnyContent] => Result . What does it mean? I can not find any examples in manuals that use several => in place of parameters list for function.

What am I missing?

Upvotes: 5

Views: 393

Answers (2)

Nicolas
Nicolas

Reputation: 24759

f is a function that, given a String will produce a function that waits for a Result[AnyContent] to provide a Result.

Then at line 2. you pass to f the userparam, which must be a String and you pass the request param to the resulting function.

This way of passing parameters is called currying. A both short and a bit more complex example can be found there: http://www.scala-lang.org/node/135

Upvotes: 4

Jesper
Jesper

Reputation: 206766

Maybe it's easier if you add some parantheses:

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

f is a call-by-name parameter; it's a function that takes a String and returns: a function that takes a Request[AnyContent] and returns a Result.

Upvotes: 7

Related Questions