tldr: I don't see how Gradle's Kotlin DSL for defining tasks is valid Kotlin and I need references to official Kotlin docs that can explain it个
The longer version
我正在阅读OpenAPI的Gradle Generator的文档,试图理解API,发现了this bit in the docs个:
// Define a task for validating one specification
task validateGoodSpec(type: org.openapitools.generator.gradle.plugin.tasks.ValidateTask) {
inputSpec.set("$rootDir/petstore-v3.0.yaml")
}
除了这不像how you register tasks in the official Gradle docs这一点之外,这看起来几乎像一个普通的函数定义(如果有人将task
替换为`Fun),但我不记得在任何"普通的"Kotlin代码中遇到过这样的语法.我知道这是Gradle DSL,但我仍然认为它应该是有效的Kotlin.这种假设是错误的吗?
Stuffing the example into IntelliJ, it could not resolve this code to the right overloaded signature of the task
function, which was a bit strange ...:
然后我tried asking ChatGPT把Kotlin代码"翻译"成纯Java.我认为这是幻觉,因为它突然增加了一个额外的Class<T>
参数,但经过一些清理,我认为结果应该是这样的,
task<ValidateTask>("validateGoodSpec") {
outputs.upToDateWhen {
false;
}
// etc ....
}
至少,这是我理解如何工作并符合现有函数签名的代码:
public open fun task(name: kotlin.String, configureAction: org.gradle.api.Action<in org.gradle.api.Task>): org.gradle.api.Task`
不过,我不知道Kotlin编译器如何神奇地将task validateGoodSpec(someAction)
变成task("validateGoodSpec", someAction)
!