1st Career
2nd Career
OCI Grails Team
Cruisin' to easy migration and interop
Caution for potholes & speed bumps
Stopping for rework or collisions
Anywhere you see a parrot means this inconsistency with Java will be changing when the new "parrot" grammar changes are available in Groovy 3.0 and maybe as options in 2.5.x
trait Singer {
int songLength
void sing() {
println "Oh say can you see..."
}
}
class Teacher implements Singer { }
class Student implements Singer {
void sing() {
println "Twinkle twinkle little star..."
}
}
The leftShift method implements << operator overload
AST transformations are compile time
return is optional
result of last statement is returned
Note: Groovy Taxable interface is nearly identical
The Order's leftShift method receives the LineItem
Triple quotes allow us to easily create formatted, multiline strings
Java 8 introduced streams, method references and lambdas
Groovy doesn't support lambdas and Java method references
== uses compareTo if Comparable, else equals()
<=> 'spaceship' operator
How are two Codes equal?
AST transformations happen at compile-time
"spaceship" operator is compareTo
Codes equal via compareTo == 0
a.is(b)
a === b
Can be written as map['Jack']
Map map = ["${name}" : 57]; println map; println map.Jack
asBoolean defines Groovy truth for the object
@CompileStatic bypasses Groovy MOP
Note Groovy method pointers
Groovy has never supported do/while
To Groovy, do { } looks like do(Closure)
until then...
Note @CompileStatic
All types in Groovy are Objects
Path file = Paths.get("/path/to/file");
Charset charset = Charset.forName("UTF-8");
try (BufferedReader reader = Files.newBufferedReader(file, charset)) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
code from http://groovy-lang.org/differences.html
new File('/path/to/file').eachLine('UTF-8') {
println it
}
// or this
new File('/path/to/file').withReader('UTF-8') { reader ->
reader.eachLine {
println it
}
}
code from http://groovy-lang.org/differences.html
Migration from Java and interop with Java is almost trivial
There are a few issues arising from the differences between Groovy and Java, but these are easy to remedy
Very few issues are irreconcilable, such as keyword conflicts.