2016-12-29 61 views
2

短的问题:我怎样才能找到一个隐含的来自Scala的地方?

有没有办法问Scala编译器来告诉我,在程序中给定的点使用的规定隐含宣布?

如果没有,是否有一个算法,我可以手动跟踪找出自己在隐式声明的位置?

长的问题:

我下面简单的喷污物tutorial

在下面的代码段(即将该this回购本教程):

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

as采用隐式FromRequestUnmarshaller[T]型的(完整的源here):

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

,当我问IntelliJ在哪里隐含来自(使用CMD + SHIFT + P),我得到:

enter image description here 当我跟随第一hint我得到这个:

trait UnmarshallerLifting { 

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

这并不能帮助我找出其中隐含FromRequestUnmarshaller[T]来自因为我想不通性状UnmarshallerLifting如何混合到QuestionResource如果我检查类层次:

enter image description here

我检查那个样子,他们可能含有这种内隐特质,例如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个不同的地方后,我变得沮丧。

有没有办法让scala编译器告诉我在一个程序中的某个给定点(在这个例子中是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

我试过了,它不会告诉我隐式来自哪里,它只会抱怨被试过但不工作的隐含:( – jhegedus

2

我这样做(在迈克尔的意见建议):

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

印刷:

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]))))))) 

这直接告诉其中隐含的是来自:Deserializer.fromRequestUnmarshaller

而且,在这里是另一种方式,通过使用InteliJ的搜索使用功能:

enter image description here