Last active
August 29, 2015 14:23
-
-
Save christoph-daehne/bbfbd29aa9cc29fb471b to your computer and use it in GitHub Desktop.
Groovy DSL example: Hello World
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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