短い質問:Scalaで暗黙的にどこから来るのかを見つける方法はありますか?
プログラム中のある時点で使用されるある種の暗黙のが宣言された場所を私に伝えるためにScalaのコンパイラを依頼する方法はありますか?
もしそうでなければ、暗黙的に宣言された場所を手動で調べるアルゴリズムはありますか?
ロング質問:
私はシンプルなスプレーCRUD 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
とき、私は尋ねますこの暗黙的な(CMD + SHIFT + Pを使用して)IntelliJの場合、私は次のようになります。
と私は最初hintに従うとき、私はこの取得:これは暗黙の
FromRequestUnmarshaller[T]
は私が点検した場合、形質UnmarshallerLifting
はQuestionResource
に混入する方法を見つけ出すことはできませんので、どこから来るのかを把握するために私を助けていない
trait UnmarshallerLifting {
implicit def fromRequestUnmarshaller[T](implicit um: FromMessageUnmarshaller[T]): FromRequestUnmarshaller[T] =
new FromRequestUnmarshaller[T] {
def apply(request: HttpRequest): Deserialized[T] = um(request)
}
...
をクラス階層:
私は例えば、彼らはこの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]
がどこから来るのか分かりません。
おそらくあなたが探している:http://stackoverflow.com/questions/34903520/figuring-out-chain-of-implicit-invocations/34903876#34903876? –
ニース、ありがとう、私はそれを見ている。 – jhegedus