This page was created to report about the changes in syntax when we switched to a new parser back in 2004. So some of the points below are not necessarily representative of what Groovy supports nowadays. Please take the notes below with a grain of salt!

Here is a checklist of changes you'll need to make to a Groovy classic codebase to ensure compatibility with the new Groovy JSR syntax.

Safe navigation

In Classic Groovy we used to use this syntax

class Person = { String name }
y = null
println "${y->name}"    // -> was the optional gpath operator with Classic Groovy syntax

Instead of using the arrow operator for safe navigation to avoid NullPointerException, we're now using the ?. operator

Now in the JSR we use this syntax

class Person = { String name }
def y = null
println "${y?.name}"    // Now, ?. is the optional gpath operator the new Groovy JSR

Parameter separator in the closure syntax

This allows us to use one single token for the separator between the closure in Classic Groovy we used to use this syntax

cl = {a| ...}
cl = {|a| ...}

Now in the JSR we use this syntax

def cl = {a -> ...}

This allows us to use one single token for the separator between the closure parameters and the code in in the closure which works with arbitrarily complex parameter list expressions and default values. e.g.

def cl = {String x = "hey", int y = a|b -> println "Values are $x and $y"}
collection.each {int item -> println item}

Property keyword

Introduction of the 'def' keyword

For Scripts (as opposed to explicitely declared classes) the syntax is not changed, i.e. variable declarations without 'def' are still allowed, because those variables are automatically created in the script binding if they don't already exist.

Introduction of the 'as' keyword

Default access level of class members

The default access level for members of Groovy classes has changed from "public" to "protected"

Classic Groovy

class Foo {
    readMe_a;
    readMe_b
}
xyz = new Foo(readMe_a:"Hello",readMe_b:"World")
println xyz.readMe_a
println xyz.readMe_b

Now in JSR Groovy

class Foo {
    public readMe_a;
    def readMe_b //def is now required because of the "def keyword" change mentioned earlier
}
xyz = new Foo(readMe_a:"Hello",readMe_b:"World")
println xyz.readMe_a
println xyz.readMe_b //errors in JSR Groovy

Array creation

// classic
args = new String[] { "a", "b" }

// JSR
String[] args = [ "a", "b" ]
def x = [1, 2, 3] as int[]
long[] y = x

float and double notation

x = .123 // classic
def x = 0.123 // JSR

This is to avoid ambiguity with things like ranges (1..2) and so forth

Explicit method pointer syntax

In classic Groovy you could access method pointers automatically if there was no java bean property of the given method name.

e.g.

// classic
methodPointer = System.out.println
methodPointer("Hello World")

This often caused confusion; as folks would use a property access to find something and get a method by accident (e.g. typo) and get confused. So now we make getting a method pointer explicit as follows

// JSR
def methodPointer = System.out.&println
methodPointer("Hello World")

def foo = ...
def p = foo.&bar

// lets call the bar method on the foo object
p(1, 2, 3)

No 'do ... while()' syntax as yet.

Due to ambiguity, we've not yet added support for do .. while to Groovy

'No Dumb Expression' rule

Markup and builders

markup (builder) { 
  // same stuff as before goes here
}

// or something like this
builder.{
  // same stuff as before goes here
}

Strings and GStrings

def foo = """
this 
is
a very
long 
string on many
lines
"""
println 'amount is $100'
// same as
println "amount is \$100"
if ('abc' =~ /.../) {}
if ('abc' ==~ /.../) {}
'abc'.eachMatch(/.../) {}
['a','b','c'].grep(/a/)

switch('abc'){
       case ~/.../ : whatever
}

assert 'EUOUAE'.matches(/^[aeiou]*$/)
assert 'EUOUAE' ==~ /^[aeiou]*$/
assert 'football'.replaceAll(/foo/, "Bar") == 'Bartball'

Assertions

assert 0 <= value : "Must be non-negative"  // classic
assert 0 <= value , "Must be non-negative"  // JSR

return/break/continue semantics in closures

NOT YET IMPLEMENTED

Integer division

Previously, in Groovy Classic, we used the backward slash as the integer division operator. This operator being confusing with escaping sequences was removed. So instead of \ please use the intdiv() method.

int result = 5 \ 3 // classic

Becomes

int result = 5.intdiv(3) // JSR

Exclusive range

The operator for creating ranges with the upper bound excluded from the range has changed.

Instead of:

range = 0...10

The syntax is now:

def range = 0..<10