简体   繁体   中英

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. 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? 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. 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. DSLs are the motivation for syntactic sugar such as this (and many other examples... it is a huge topic).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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