What I learned
AST transformation
Before I did not aware the feature of AST transformation in Groovy, probably this is the new feature, because I did not find this topic in the book.
The author covered @Immutable, @Newify, and @Delegate
@Immutable
@Immutable class Person { String firstName; String lastName; int age; }@Immutable annotation applies to class level only, it does not support on the field level.
@Newify
@Newify def createPerson { Person.new("John", "Smith", 28) } @Newify(Person) def createPerson { Person("John", "Smith", 28) }
@Delegate
class TirelessWorker { public void work() {} public void report() {} } class Manager { @Delegate employee = new TirelessWorker(); public void schedule() {} }
Here the Manager class can use all the methods of TirelessWorker class, if you add/modify method of class Tirelessworker, you don't have to modify the Manager class, it follows the OCP principle.
Multimethods
Multimethods is one of the major difference between Groovy and Java. Multimethod means the method call depends on the target and the parameter type, which reminds me the multiple dispatch, Java does not support mutlimethod, it only support single dispatch, I remember when I learn the Visitor pattern, I learned the multiple dispatch term, I am sure the Visitor pattern will be much easier to understand if the language support multiple dispatch, cool.
ArrayList
use spread-dot operator to access ArrayList
*. is called spread-dot operator, for example:
list*.member = list.collect { item -> item?.member }
It means given a list, iterate each item's specific method, form the return value as a collection.
get each item's first name: list*.firstName
Groovy follows the Kanban principle
I think Groovy is a good example of evolutionary and emergent design, since you can begin with purely java code, then migrate with Groovy, and even more you can mix with java and Groovy code in your project. Let's look at the Kanban's 2 foundational principles: Start with what you do now; Agree to pursue incremental, evolutionary change. Pretty similar.
Based on this, I think Groovy is an important language that worth every Java developer to looking at.