Skip to content

Instantly share code, notes, and snippets.

@christoph-daehne
Last active October 20, 2016 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christoph-daehne/f3fc7a4122ac29749475 to your computer and use it in GitHub Desktop.
Save christoph-daehne/f3fc7a4122ac29749475 to your computer and use it in GitHub Desktop.
Highlighted Groovy DSL example: Hello World
class HelloWorldService {
void printHelloWorld() {
println("Hello World")
}
}
class HelloWorldScript {
// by annotating the parameter we gain syntax highlighting and auto-completion
static void execute(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = HelloWorldService) Closure script) {
script.resolveStrategy = Closure.DELEGATE_FIRST
script.delegate = new HelloWorldService()
script()
}
}
HelloWorldScript.execute {
printHelloWorld()
for(def i = 0; i < 10; i++) {
printHelloWorld()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment