这是一个草编程序,它说明了我遇到的问题
trait RequestBuilder {
type Out
def complete(p: Promise[Out]): Unit
}
def makeRequest(in: RequestBuilder): Source[(RequestBuilder, Promise[in.Out]), Future[in.Out]] = {
val p = Promise[in.Out]
Source.single(in -> p).mapMaterializedValue(_ => p.future)
}
val sink = MergeHub.source[(RequestBuilder, Promise[???])].to(Sink.foreach {
case (r, p) => r.complete(p)
}).run()
sink.runWith(makeRequest(new RequestBuilder {
type Out = Int
def complete(p: Promise[Out]): Unit = p.success(1)
}))
问题是,如何在接收器中键入Promise[???]
?我已经能够通过使Promise成为RequestBuilder特性本身的一部分来解决这个问题,但这对我来说似乎是一种代码味道
答案 0 :(得分:0)
您可以为元组创建一个类型别名:
type BuilderWithPromise[B <: RequestBuilder] = (B, Promise[B#Out])
或案例类:
case class BuilderWithPromise[B <: RequestBuilder)(
builder: B,
promise: Promise[B#Out]
)
无论哪种方式都应该起作用