【发布时间】:2017-05-13 22:31:29
【问题描述】:
小问题:
有没有办法让 scala 编译器告诉我在程序中给定点使用的某个隐式声明的位置?
如果没有,是否有一种算法可以让我手动查找隐式声明的位置?
长问题:
我正在关注简单的喷雾剂tutorial。
在下面的代码 sn-p 中(来自教程的this repo):
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)时,我得到:
当我关注第一个 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 中的:
我检查了看起来可能包含此隐含的特征,例如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]来自哪里。
【问题讨论】:
-
也许您正在寻找:stackoverflow.com/questions/34903520/… ?
-
很好,谢谢,我看看。
标签: scala intellij-idea implicit spray