简体   繁体   English

这个Groovy语法叫什么(例如,Gradle'exclude'闭合语法)?

[英]What is this Groovy syntax called (Gradle 'exclude' closure syntax, e.g.)?

I feel like I've seen the term for this somewhere, but I haven't been able to find it on the web or SO. 我觉得我已经在某个地方看到过这个词,但是我还没在网上或SO上找到它。 What is the name for the Groovy syntax that makes it possible to append the closure after the compile method in a Gradle dependencies closure, for example? 例如,Groovy语法的名称是什么,它使得可以在Gradle dependencies闭包中的compile方法之后附加闭包? How does it work? 它是如何工作的? How would I write a function that uses this syntax? 如何编写使用此语法的函数?

compile ('org.springframework.boot:spring-boot-starter-web') {
  exclude group: 'org.slf4j', module: 'slf4j-api'
}

Thanks! 谢谢!

Groovy has a flexible syntax for passing a closure as the last parameter to a method. Groovy具有灵活的语法,可以将闭包作为最后一个参数传递给方法。 Consider: 考虑:

def myCompile (a, b, c) {
    println c(a,b)
}

myCompile(10, 20, { x, y -> x + y })

myCompile(10, 20) { x, y ->
    x + y
}

I don't think this has a name, but more generally the Gradle build.gradle syntax forms a DSL (Domain Specific Language) which is fluid and natural. 我不认为这有个名字,但更一般而言,Gradle build.gradle语法形成了一种流畅自然的DSL (特定于域的语言)。 DSLs are the motivation for syntactic sugar such as this (and many other examples... it is a huge topic). DSL是诸如此类的语法糖的动机(以及许多其他示例……这是一个巨大的话题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM