Reputation: 6003
In the file https://github.com/playframework/playframework/blob/master/framework/src/play/src/main/scala/play/api/mvc/Security.scala, it has the following example code in a comment:
//in a Security trait
def username(request: RequestHeader) = request.session.get("email")
def onUnauthorized(request: RequestHeader) = Results.Redirect(routes.Application.login)
def isAuthenticated(f: => String => Request[AnyContent] => Result) = {
Authenticated(username, onUnauthorized) { user =>
Action(request => f(user)(request))
}
}
//then in a controller
def index = isAuthenticated { username => implicit request =>
Ok("Hello " + username)
}
So I tried to write some similar code:
def test1(a: =>Int=>Int=>Int):String="1"
test1 {1 => 1 => 1} // But this line doesn't compile.
I called it the same way as isAuthenticated
is called, so why doesn't my code compile?
Upvotes: 0
Views: 116
Reputation: 13667
test1 {1 => 1 => 1}
doesn't compile because {1 => 1 => 1}
isn't a valid function definition. {x => y => 1}
, on other hand, does compile. This is because 1
cannot be used as the name for a parameter.
Upvotes: 1
Reputation: 55569
Take a look at what happens when you paste def test1(a: =>Int=>Int=>Int):String="1"
into the Scala REPL:
test1: (a: => Int => (Int => Int))String
test
becomes defined as a function that accepts an another function as it's parameter. Notice the extra parenthesis in the REPL output. a
is a function that maps Int
to another function Int => Int
.
So something like this would be more appropriate:
def a(i: Int): Int => Int = {j: Int => i + j}
test1(a)
Upvotes: 1