我正在开发一个http4k网络应用程序。Http4k为 http 处理程序和过滤器(又名拦截器)提供了一个很好的功能方案。
typealias HttpHandler = (Request) -> Response
interface Filter : (HttpHandler) -> HttpHandler {...}
我想写一个简单的过滤器,所以我创建了一个函数,它返回一个Filter
fun throwNotFoundResponses(): Filter {
return { next: HttpHandler ->
{ request: Request ->
val response = next(request)
if (response.status == Status.NOT_FOUND) {
throw NotFoundException()
}
response
}
}
}
// example usage
Filter.NoOp
.then(throwNotFoundResponses())
.then(routes(...))
然而 Kotlin 抱怨(编辑行号以匹配上面的示例。)
NotFoundThrower.kt: (2, 12): Type mismatch: inferred type is (HttpHandler /* = (Request) -> Response */) -> (Request) -> Response but Filter was expected
为什么 Kotlin 不能推断类型实际上是相同的?