Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 609
Oleg Avatar asked Mar 27 '12 13:03

Oleg


2 Answers

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.

like image 140
Jesper Avatar answered Oct 01 '22 21:10

Jesper


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

like image 41
Nicolas Avatar answered Oct 01 '22 19:10

Nicolas