Book Image

Groovy for Domain-Specific Languages, Second Edition

By : Fergal Dearle
Book Image

Groovy for Domain-Specific Languages, Second Edition

By: Fergal Dearle

Overview of this book

Table of Contents (20 chapters)
Groovy for Domain-specific Languages Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Introduction to DSLs and Groovy
Index

Adding built-in methods


However simple our DSL might now look, the need to preface our method calls with GeeTwitter is one final piece of boilerplate code that it would be nice to remove. In some of the previous examples we have used a static import to do this, but it is still not very intuitive to a non Java/Groovy programmer why we are doing this. Since we are evaluating the DSL script ourselves, rather than allowing Groovy to do it, we still have some scope to do this.

It would certainly be nicer to be able to write:

eachFollower { 
  sendMessage it, "Thanks for taking the time to follow me!"
}

This assumes that the eachFollower method is built in to the DSL, rather than the more verbose:

GeeTwitter.eachFollower { 
  GeeTwitter.sendMessage (it, 
         "Thanks for taking the time to follow me!")
}

Groovy provides two mechanisms that allow us achieve just this. Later in the book, we will look at how we can manipulate the binding to achieve this. For this chapter, we will look at a more straightforward...