加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何找到Scala中隐含的来源?

发布时间:2020-12-16 18:13:06 所属栏目:安全 来源:网络整理
导读:简短的问题: 有没有办法让scala编译器告诉我在程序的给定点上使用了某个隐式? 如果没有,是否有一个算法,我可以手动跟踪,找出自己声明隐含的位置? 长问题: 我正在关注简单的喷雾tutorial. 在下面的代码片段中(来自教程的this repo): pathEnd { post { en
简短的问题:

有没有办法让scala编译器告诉我在程序的给定点上使用了某个隐式?

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

长问题:

我正在关注简单的喷雾tutorial.

在下面的代码片段中(来自教程的this repo):

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

采用隐式类型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,但它不包含隐含的:

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]来自哪里.

解决方法

通常我在编译器中启用-Xlog-implicits以查看implicits发生了什么.

另外,不赞成使用akka-http.我建议切换.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读