考虑到 bindAndHandle 方法采用 Flow 而不是 Route 类型的第一个参数,Scala 程序如何编译?

Nic*_*oiu 0 scala akka akka-http

在一个演示项目中,https://github.com/chbatey/akka-http-typed存在以下代码,它可以编译但我不明白表达式“Http()(untypedSystem).bindAndHandle(routes .userRoutes, "localhost", 8080)" 可以编译,因为 bindAndHandle 方法接受下面的参数(所以第一个应该是 Flow,但它是一个 Route,即 RequestContext ? Future[RouteResult] 函数类型)。

在另一个项目中,我得到了编译错误,我可以理解,因为实际参数的类型为Route,但第一个参数的声明类型为Flow。

请指教。

//def bindAndHandle(
//    handler:   Flow[HttpRequest, HttpResponse, Any],
//    interface: String, port: Int = DefaultPortForProtocol,
//    connectionContext: ConnectionContext = defaultServerHttpContext,
val serverBinding: Future[Http.ServerBinding] = Http()(untypedSystem).bindAndHandle(routes.userRoutes, "localhost", 8080)
//type of routes.userRoutes is Route
//where type Route = RequestContext ? Future[RouteResult]
//how can it be that this compiles? In another project it does not (which makes more sense to me)
Run Code Online (Sandbox Code Playgroud)

Tim*_*Tim 5

在类型的伴生对象中有一个从Route到的隐式转换,称为。Flow[HttpRequest, HttpResponse, NotUsed]RouteResultroute2HandlerFlow

目前尚不清楚为什么在您的其他项目中没有发现这一点,但如果它仍然是一个问题,请将其作为一个单独的问题发布。