簡體   English   中英

下划線是什么意思?

[英]What's meaning of this underscore?

我正在閱讀akka-http的源代碼,這是akka.http.scaladsl.server.directives.RouteDirectives源代碼,以complete方法為例,誰能說出StandardRoute(_.complete(m))中的下划線是什么意思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())
}

StandardRoute(_.complete(m))等同於StandardRoute(x => x.complete(m))

下划線是x 如果您想知道Scala中下划線的用例。 請檢查此鏈接(Scala中下划線的使用)

這是來自akka http庫的代碼

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 }
}

路線不過是功能

type Route = RequestContext ⇒ Future[RouteResult]

說明:

考慮該對象編號。 該對象的apply方法具有一個功能。 現在查看如何使用此Number對象。

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

用法:

Number(_.toInt)

要么

Number(x => x.toInt)

斯卡拉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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM