我正在尝试了解 Kotlin / Ktor 中的 HTML 构建器。此处的示例使用 HTML 构建器来构建结果:
call.respondHtml {
head {
title { +"HTML Application" }
}
body {
h1 { +"Sample application with HTML builders" }
widget {
+"Widgets are just functions"
}
}
}
我正在尝试将主体提取到这样的变量中:
val block: HTML.() -> Unit = {
head {
title { +"HTML Application" }
}
body {
h1 { +"Sample application with HTML builders" }
}
}
call.respondHtml(block)
现在我得到以下编译错误:
Error:(37, 22) Kotlin: None of the following functions can be called with the arguments supplied:
public suspend fun ApplicationCall.respondHtml(status: HttpStatusCode = ..., versions: List<Version> = ..., cacheControl: CacheControl? = ..., block: HTML.() -> Unit): Unit defined in org.jetbrains.ktor.html
public suspend fun ApplicationCall.respondHtml(status: HttpStatusCode = ..., block: HTML.() -> Unit): Unit defined in org.jetbrains.ktor.html
当我添加第一个(可选)参数时,它再次起作用:call.respondHtml(HttpStatusCode.OK, block)
.
当我只是尝试将主体提取到变量中时,为什么它不起作用?