【发布时间】:2020-01-15 08:09:33
【问题描述】:
我有一个构建在 Play/Lagom 堆栈之上的应用程序。我需要调用一个需要Source[T, NotUsed] 的服务来将文件流式传输到它。这是服务的接口:
def foo(fooId: UUID): ServiceCall[Source[ByteString, NotUsed], Source[String, NotUsed]]
因此,我通过以下方式使用 Play 文档中的 Accumulator.source:
private def doSomething(fooId: UUID): BodyParser[Future[Seq[String]]] = BodyParser { _ =>
Accumulator.source[ByteString]
.mapFuture { source: Source[ByteString, NotUsed] =>
externalService
.foo(fooId)
.invoke(source)
.map { x =>
Right(x.runWith(Sink.seq[String]))
}
}
}
现在,在 mapFuture 调用中,source 的类型是 Source[ByteString, _],但如果我将其更改为 Source[ByteString, NotUsed] 以便调用服务,如上例所示,我会得到一个IDE 中的错误
Source[ByteString, _] 是预期的。 _ 不应该意味着我可以将类型更改为我想要的任何类型,包括NotUsed?另一方面,这两者在语义上不应该是等价的吗?我发现 Akka 团队引入了 akka.NotUsed 来替换 Unit,因为在某些以前的版本中物化值无关紧要,但这仍然没有给我提供如何解决我的问题的线索。
上面的sn-p类似于Play documentation on directing the body elsewhere.中的这个例子
【问题讨论】:
标签: scala playframework akka covariance existential-type