Skip to content

Instantly share code, notes, and snippets.

@christoph-daehne
Last active August 29, 2015 14: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/bbfbd29aa9cc29fb471b to your computer and use it in GitHub Desktop.
Save christoph-daehne/bbfbd29aa9cc29fb471b to your computer and use it in GitHub Desktop.
Groovy DSL example: Hello World
class HelloWorldService {
// this function becomes a keyword in our DSL
void printHelloWorld() {
println("Hello World")
}
}
class HelloWorldScript {
static void execute(Closure script) {
// here we wire the colure with the service
script.delegate = new HelloWorldService()
// search the delegate for variables and methods
// if they do not exist search the scope of the closures definition
script.resolveStrategy = Closure.DELEGATE_FIRST
// the closure is executed in the context of its delegate
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