1

I am reading akka-http source codes, here is source code of akka.http.scaladsl.server.directives.RouteDirectives, take the complete method as example, can anybody tell what's meaning of the underscore in StandardRoute(_.complete(m))?

package akka.http.scaladsl.server
package directives

import akka.http.scaladsl.marshalling.ToResponseMarshallable
import akka.http.scaladsl.model._
import StatusCodes._

/**
 * @groupname route Route directives
 * @groupprio route 200
 */
trait RouteDirectives {

  ....
  ....

  /**
   * Completes the request using the given arguments.
   *
   * @group route
   */
  def complete(m: ⇒ ToResponseMarshallable): StandardRoute =
    StandardRoute(_.complete(m))
}

object RouteDirectives extends RouteDirectives {
  private val _reject = StandardRoute(_.reject())
}
Laurence Geng
  • 424
  • 3
  • 9

1 Answers1

2

StandardRoute(_.complete(m)) is equivalent to StandardRoute(x => x.complete(m))

Here underscore refers to x. If you want to know the use cases of underscore in scala. Please check this link (Uses of underscore in Scala)

Here is the code from akka http library

object StandardRoute {
  def apply(route: Route): StandardRoute = route match {
    case x: StandardRoute ⇒ x
    case x                ⇒ new StandardRoute { def apply(ctx: RequestContext) = x(ctx) }
  }

  /**
   * Converts the StandardRoute into a directive that never passes the request to its inner route
   * (and always returns its underlying route).
   */
  implicit def toDirective[L: Tuple](route: StandardRoute): Directive[L] =
    Directive[L] { _ ⇒ route }
}

Route is nothing but function

type Route = RequestContext ⇒ Future[RouteResult]

Explanation:

Consider this object Number. This object's apply method takes a function. Now see how this Number object will be used.

object Number {
 def apply(f: String => Int): Int = {
  f("123") 
 }
}

Usage:

Number(_.toInt)

or

Number(x => x.toInt)

Scala REPL

scala> object Number {
     |      def apply(f: String => Int): Int = {
     |       f("123") 
     |      }
     |     }
defined object Number

scala> Number(_.toInt)
res0: Int = 123

scala> Number(x => x.toInt)
res1: Int = 123
Community
  • 1
  • 1
Nagarjuna Pamu
  • 14,737
  • 3
  • 22
  • 40