Comparing Java to Groovy NFJS talk online
The slides for my NFJS talk “Comparing Java and Groovy”, along with source code are now online in the Presentations section.
Matt Secoske’s intermittent ramblings on software and life
{ Category Archives }
The slides for my NFJS talk “Comparing Java and Groovy”, along with source code are now online in the Presentations section.
I have been doing a lot of digging into Groovy and DSLs lately, getting ready for my OSCON talk next week.
The Groovy language has great built-in support for creating DSLs. Along with its dynamic nature, Groovy has the “use” keyword for working with categories, and coming in 1.1, the ExpandoMetaClass if you want to complete.
Categories help you do scoped “extension” to normal every day objects. An example is in order:
So, inside the use block, there is now (effectively) a new property on the Integer class, called tacos, that will return a string describing your delectable entree. This is a very powerful technique, and is very useful for developing your own expressive syntax.
One drawback to this technique is that you have to define where you are using it. Use can be extended into closures, but does not always extend into predefined methods.
I was talking about Groovy Categories with Blaine Buxton, and was telling him that I did not like having the “use” block inside my DSL code, and wanted a more transparent approach. He suggested that perhaps an Annotation and an Aspect would do what I was looking for.
With the 1.1-Beta1 Release, Groovy has built in support for Java Annotations, and since Groovy compiles down to byte code, there is implicit support for Aspects. Putting these two things together allows us to create an annotation for a more powerful use:
With a simple annotation, @Uses(category=[list of category classes]), we now can define them for any method we want, without having to put them into the method body.
Here is the Annotation code:
And here is the Aspect code:
One thing to note when working with Annotations / Aspects in Groovy: you have to keep track of the build order. I struggled for a while to figure out why I was not seeing the annotation in my Groovy classes. It was because it had not been built yet! I ended up having to split my compilation into 4 parts:
The other day I needed to remove a mess of .svn directories from a project as I moved it from my personal box (using svn as the soure control) to the client’s source control system. This is a fairly frequent task, so attempting to be a “productive programmer” I decided that I would take the time to write a script to do this for me. After testing, it took about an hour to write… mainly because I tried to be too fancy, and tried a few things in Groovy I had not seen before. Overall, it was an hour well spent:
14 lines. I really like the eachDir and the eachFile methods on the File object… and there is even an eachFileRecurse. All I can say is: where the heck is the eachDirRecurse??? Oh well. It was still a good learning experience, and now I have a malleable tool next time I run into something similar.
Earlier today I put GroovyQuiz.com online, along with its first quiz (Anyone up for a game of Life?)
It amazing to see the response its getting already… there are a number of sites linking to it already (Thank you aboutGroovy.com and groovyblogs.org… you both rock!). And I’ve got a number of solutions (one in under 3 hours!) to post as soon as I finish up that section of the site.