Today I stumbled over a AST transformation that I'd never noticed before, but I'm sure I'm going to use it frequently from now on. A simple way to make two instances with the same values equal.
http://groovy.codehaus.org/gapi/groovy/transform/EqualsAndHashCode.html
import groovy.transform.EqualsAndHashCode
 @EqualsAndHashCode
 class Person {
     String first, last
     int age
 }
 def p1 = new Person(first:'John', last:'Smith', age:21)
 def p2 = new Person(first:'John', last:'Smith', age:21)
 assert p1 == p2
 def map = [:]
 map[p1] = 45
 assert map[p2] == 45
Now that's cool.

 
Keine Kommentare:
Kommentar veröffentlichen