【问题标题】:Where does this akka-http test import Marshaller?这个 akka-http 测试在哪里导入 Marshaller?
【发布时间】:2017-07-07 23:24:43
【问题描述】:

我正在查看这个 akka-http 文档示例(链接到 akka-http 测试代码):

Marshalling

这实际上是来自github代码MarshalSpec.scala的测试。我的问题是,这里导入的隐式 Marshaller 在哪里?我正在看进口,但我找不到它?我尝试使用 IntelliJ 向我展示隐式导入,但我仍然无法弄清楚这一点。获取传递给 Marshaller 的隐式声明的 import 语句在哪里:

  val entityFuture = Marshal(string).to[MessageEntity]

在第 21 行? 它调用了

def to[B](implicit m: Marshaller[A, B], ec: ExecutionContext): Future[B] =

在 Marshal.scala 中并传递一个隐含的 m: Marshaller,我无法确定。

【问题讨论】:

    标签: scala akka-http


    【解决方案1】:

    Marshaller 扩展 PredefinedToEntityMarshallers ,提供从字符串编组。

    但是,为什么 Marshaller 带有自己的隐式?这是因为 scala 在类型参数的隐式范围内搜索隐式:Where does Scala look for implicits?

    所以,Marshaller 自带了自己的 Marshaller,随时可以使用 :)

    【讨论】:

    • 啊啊,类型的伴生对象!!!我忘记了这一点,并试图在当前范围内找到它。 object Marshaller extends GenericMarshallers with PredefinedToEntityMarshallers with PredefinedToResponseMarshallers with PredefinedToRequestMarshallers { ....
    猜你喜欢
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多