这个下划线是什么意思?
What's meaning of this underscore?
我在看akka-http的源码,这里是akka.http.scaladsl.server.directives.RouteDirectives
的源码,以complete
方法为例,谁能告诉我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
。如果你想知道 underscore 在 scala 中的用例。请检查 this link (Uses of underscore in 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)
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
我在看akka-http的源码,这里是akka.http.scaladsl.server.directives.RouteDirectives
的源码,以complete
方法为例,谁能告诉我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
。如果你想知道 underscore 在 scala 中的用例。请检查 this link (Uses of underscore in 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)
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