Gradle Kotlin DSL 中的 closureOf 和 delegateClosureOf 有什么区别

What is the difference between closureOf and delegateClosureOf in Gradle Kotlin DSL

The official doc 说:

You may sometimes have to call Groovy methods that take Closure arguments from Kotlin code. For example, some third-party plugins written in Groovy expect closure arguments.

In order to provide a way to construct closures while preserving Kotlin’s strong typing, two helper methods exist:

  • closureOf<T> {}
  • delegateClosureOf<T> {}

Both methods are useful in different circumstances and depend upon the method you are passing the Closure instance into. Some plugins expect simple closures. In other cases, the plugin expects a delegate closure. There sometimes isn’t a good way to tell, from looking at the source code, which version to use. Usually, if you get a NullPointerException with closureOf<T> {}, using delegateClosureOf<T> {} will resolve the problem.

好吧,我并不反对尝试失败修复方法,但也许有一种确定性的方法可以提前告诉使用哪种方法以及为什么?

but maybe there is a deterministic way to tell in advance which method to use

当然可以,只需检查您正在配置的插件的源代码即可。比如他们的Bintray插件例子是:

bintray {
    pkg(closureOf<PackageConfig> {
        // Config for the package here
    })
}

如果您要检查来源,您会发现:https://github.com/bintray/gradle-bintray-plugin/blob/master/src/main/groovy/com/jfrog/bintray/gradle/BintrayExtension.groovy#L35..L37

def pkg(Closure closure) {
    ConfigureUtil.configure(closure, pkg)
}

这是一个简单Closure,所以根据文档,closureOf<T> {}在这里是合适的。

现在他们的另一个例子是配置农场时的 Gretty 插件:

farms {
    farm("OldCoreWar", delegateClosureOf<FarmExtension> {
        // Config for the war here
    })
}

如果您检查来源,您会发现:https://github.com/akhikhl/gretty/blob/master/libs/gretty-core/src/main/groovy/org/akhikhl/gretty/FarmsConfig.groovy#L23..L32

  void farm(String name = null, Closure closure) {
    if(name == null)
      name = ''
    def f = farmsMap_[name]
    if(f == null)
      f = farmsMap_[name] = createFarm()
    closure.delegate = f
    closure.resolveStrategy = Closure.DELEGATE_FIRST
    closure()
  }

根据文档,这比前面的示例复杂得多,因为这显然需要委托关闭,所以 delegateClosureOf<T> {} 将是合适的选择。