1

Can anyone please help me to understand below scala syntax?

def index = withAuth {
  implicit request => userId =>
    Ok(views.html.app.index())
}

Syntax taken from here.

My understanding is: withAuth is an Action and request is an input to anonymous function. However I can't understand

  1. Two right hand operators (=>)
  2. From where it will receive userId value?
  3. Is userId also an input parameter to anonymous function?

Thanks

Ende Neu
  • 15,581
  • 5
  • 57
  • 68
Nilesh
  • 43
  • 4

1 Answers1

1

It's a just an anonymous curried function. Function with single argument that returns function with single argument.

// anonymous function that returns function:
implicit request => {
  val inner = userId: UserIdType => Ok(views.html.app.index())
  inner
}

// inline `inner` and use type inference for UserIdType:
implicit request => {
  userId => Ok(views.html.app.index())
}


// remove curly brackets for single expression result:
implicit request =>
  userId => Ok(views.html.app.index())

One can call such function this way:

curriedFunction(a)(b)

Type of withAuth parameter is like this RequestType => UserIdType => ResultType. It allows you to make request as implicit. See this answer.

Community
  • 1
  • 1
senia
  • 37,745
  • 4
  • 88
  • 129
  • Thanks for detail lovely explanation :).Syntax to call the function helped me to understand more. – Nilesh Jan 12 '15 at 17:49