这一段Kotlin为什么没有报错

fun Application.main() {
    routing {
        static {
            defaultResource("index.html", "web")
            resources("web")
        }
    }
}                

以下API来源于ktor

fun Application.routing(configuration: Routing.() -> Unit): Routing
fun Route.static(configure: Route.() -> Unit): Route

也就是说,调用static方法返回的是一个Route,而routing接受函数类型的返回值为Unit,为啥不报错呢

阅读 1.1k
1 个回答

想报错的话,你应该这样改写你的代码

fun Application.main() {
    routing {
        return static {
            defaultResource("index.html", "web")
            resources("web")
        }
    }
}