2016-11-01 8 views
1

私はakka-httpソースコードを読んでいます、ここではのソースコードです。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()) 
} 

答えて

2

StandardRoute(_.complete(m))x指す下線ここStandardRoute(x => x.complete(m))

と等価です。スカラーのアンダースコアの使用例を知りたい場合。 this link (Uses of underscore in Scala)

をここで確認してくださいアッカ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 
+0

いいえ、StandardRouteの適用方法は、ルートインスタンスを受け付けます。 –

+0

@LaurenceGengルート()M(_。完全) – pamu

+0

@LaurenceGeng 'StandardRouteが' 'StandardRoute(X => x.complete(M))に置き換えることができ、次に機能を拡張しなければならない' – pamu

関連する問題