如果 bindAndHandle 方法采用 Flow 而非 Route 类型的第一个参数,Scala 程序如何编译?
How does the Scala program compile, given that bindAndHandle method takes the first parameter of type Flow not Route?
在一个演示项目中,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)
在 RouteResult
类型的伴随对象中存在从 Route
到 Flow[HttpRequest, HttpResponse, NotUsed]
的隐式转换,称为 route2HandlerFlow
。
不清楚为什么在您的其他项目中没有发现这个问题,但是post如果它仍然是一个问题,那么作为一个单独的问题。
在一个演示项目中,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)
在 RouteResult
类型的伴随对象中存在从 Route
到 Flow[HttpRequest, HttpResponse, NotUsed]
的隐式转换,称为 route2HandlerFlow
。
不清楚为什么在您的其他项目中没有发现这个问题,但是post如果它仍然是一个问题,那么作为一个单独的问题。