8

I have a directive, defined like

def allowedRoles(roles: UserRole*)(implicit login: Login): Directive0 = ???

But I don;t seem to be able to use it in without having to explicitly pass in the login parameter

def myRoutes(implicit req: HttpRequest, login: Login) = {
  path("example" / "path") {
    get {
      allowedRoles(Administrator) { // ← fails 
        handleGet
      }
    }
  }
}

if I try to compile this it fails with a type mismatch:

[error]  found   : akka.http.scaladsl.server.Route
[error]     (which expands to)  akka.http.scaladsl.server.RequestContext => scala.concurrent.Future[akka.http.scaladsl.server.RouteResult]
[error]  required: com.example.Login
[error]         allowedRoles(Administrator) { handleGet } }

if I change the marked line to allowedRoles(Administrator)(login) then it works, but it seems like I should not need to do this, what am I missing?

Ian Phillips
  • 567
  • 4
  • 13

1 Answers1

12

This happens because by Scala rules, { handleGet } is considered the second parameter list of allowedRoles. To fix this, make it clear it's actually the parameter of Directive0.apply:

allowedRoles(Administrator).apply { handleGet }
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487