2016-12-29 9 views
2

短い質問:Scalaで暗黙的にどこから来るのかを見つける方法はありますか?

プログラム中のある時点で使用されるある種の暗黙のが宣言された場所を私に伝えるためにScalaのコンパイラを依頼する方法はありますか?

もしそうでなければ、暗黙的に宣言された場所を手動で調べるアルゴリズムはありますか?

ロング質問:

私はシンプルなスプレーCRUD tutorial次ています。 (チュートリアルthisレポ来る)以下のコードで

pathEnd { 
    post { 
    entity(as[Question]) { question => 
     completeWithLocationHeader(
     resourceId = questionService.createQuestion(question), 
     ifDefinedStatus = 201, ifEmptyStatus = 409) 
     } 
    } 
} ~ 

asFromRequestUnmarshaller[T](完全なソースhere)の暗黙をとる:

def as[T](implicit um: FromRequestUnmarshaller[T]) = um 

とき、私は尋ねますこの暗黙的な(CMD + SHIFT + Pを使用して)IntelliJの場合、私は次のようになります。

enter image description here と私は最初hintに従うとき、私はこの取得:これは暗黙のFromRequestUnmarshaller[T]は私が点検した場合、形質UnmarshallerLiftingQuestionResourceに混入する方法を見つけ出すことはできませんので、どこから来るのかを把握するために私を助けていない

trait UnmarshallerLifting { 

    implicit def fromRequestUnmarshaller[T](implicit um: FromMessageUnmarshaller[T]): FromRequestUnmarshaller[T] = 
    new FromRequestUnmarshaller[T] { 
     def apply(request: HttpRequest): Deserialized[T] = um(request) 
    } 
... 

をクラス階層:

enter image description here

私は例えば、彼らはこのimplictが含まれている可能性があるように見える特徴、this形質を調べ、それが暗黙的に含まれていません:

trait MarshallingDirectives { 
    import BasicDirectives._ 
    import MiscDirectives._ 
    import RouteDirectives._ 

    /** 
    * Unmarshalls the requests entity to the given type passes it to its inner Route. 
    * If there is a problem with unmarshalling the request is rejected with the [[spray.routing.Rejection]] 
    * produced by the unmarshaller. 
    */ 
    def entity[T](um: FromRequestUnmarshaller[T]): Directive1[T] = 
    extract(_.request.as(um)).flatMap[T :: HNil] { 
     case Right(value)       ⇒ provide(value) 
     case Left(ContentExpected)     ⇒ reject(RequestEntityExpectedRejection) 
     case Left(UnsupportedContentType(supported)) ⇒ reject(UnsupportedRequestContentTypeRejection(supported)) 
     case Left(MalformedContent(errorMsg, cause)) ⇒ reject(MalformedRequestContentRejection(errorMsg, cause)) 
    } & cancelAllRejections(ofTypes(RequestEntityExpectedRejection.getClass, classOf[UnsupportedRequestContentTypeRejection])) 

    /** 
    * Returns the in-scope FromRequestUnmarshaller for the given type. 
    */ 
    def as[T](implicit um: FromRequestUnmarshaller[T]) = um 

    /** 
    * Uses the marshaller for the given type to produce a completion function that is passed to its inner route. 
    * You can use it do decouple marshaller resolution from request completion. 
    */ 
    def produce[T](marshaller: ToResponseMarshaller[T]): Directive[(T ⇒ Unit) :: HNil] = 
    extract { ctx ⇒ (value: T) ⇒ ctx.complete(value)(marshaller) } & cancelAllRejections(ofType[UnacceptedResponseContentTypeRejection]) 

    /** 
    * Returns the in-scope Marshaller for the given type. 
    */ 
    def instanceOf[T](implicit m: ToResponseMarshaller[T]) = m 

    /** 
    * Completes the request using the given function. The input to the function is produced with the in-scope 
    * entity unmarshaller and the result value of the function is marshalled with the in-scope marshaller. 
    */ 
    def handleWith[A, B](f: A ⇒ B)(implicit um: FromRequestUnmarshaller[A], m: ToResponseMarshaller[B]): Route = 
    entity(um) { a ⇒ RouteDirectives.complete(f(a)) } 
} 

object MarshallingDirectives extends MarshallingDirectives 

20の異なる場所を見た後、私はイライラします。

プログラム内の特定のポイント(この例ではhere)で使用されている特定の暗黙的な(この例ではFromRequestUnmarshaller[T])が宣言された場所を教えてもらえますか?

もしそうでなければ、暗黙的に宣言された場所を手動で調べるアルゴリズムはありますか?

私はGoogle/SOFでこの質問を探しましたが、私が見つけたヒントは助けになりませんでした。私もthisに行きました、そして、私はまだFromRequestUnmarshaller[T]がどこから来るのか分かりません。

+1

おそらくあなたが探している:http://stackoverflow.com/questions/34903520/figuring-out-chain-of-implicit-invocations/34903876#34903876? –

+0

ニース、ありがとう、私はそれを見ている。 – jhegedus

答えて

4

通常、コンパイラで-Xlog-implicitsを有効にして、暗黙のうちに何が起こっているのかを確認できます。

また、スプレーはakka-httpの方が推奨されていません。私は切り替えることをお勧めします。

+0

私はそれを試しました、それは暗黙のどこから来ている私に教えていない、それは試して働いていないimplicitsについてのみ不平を言う:( – jhegedus

2

私はこの(マイケルさんのコメントで提案されているように)でした:それは印刷

import scala.reflect.runtime.universe.reify 
    println(reify(entity(as[Question]))) 

:こちらを、またDeserializer.fromRequestUnmarshaller

:暗黙のはどこから来ている

Expr[spray.routing.Directive1[spray_examples.plain_rest.danielasfregola.quiz.management.entities.Question]](QuestionResource.entity(QuestionResource.as[Question](Deserializer.fromRequestUnmarshaller(Deserializer.fromMessageUnmarshaller(QuestionResource.json4sUnmarshaller(ManifestFactory.classType(classOf[spray_examples.plain_rest.danielasfregola.quiz.management.entities.Question]))))))) 

これは直接伝えます他の方法で、InteliJの検索使用機能を使用します。

enter image description here

関連する問題