Skip to end of metadata
Go to start of metadata

Warning

Icon

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

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

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

Now in the JSR we use this syntax

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.

Property keyword

  • property keyword has been replaced with an annotation

Introduction of the 'def' keyword

  • local variable declarations and fields currently need to be specified with 'def', a modifier, and/or a type. e.g.
    (Syntactically, the new keyword 'def' acts for methods as a modifier like 'public'.)

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

  • We can change types of objects with using the 'as' keyword,
    e.g.

Default access level of class members

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

Classic Groovy

Now in JSR Groovy

Array creation

  • no special array syntax. To make the language much cleaner, we now have a single syntax to work with lists and arrays in the JSR. Also note that we can now easily coerce from any collection or array to any array type
  • Be careful: we don't support native multi-dimensional array creation right now.

float and double notation

  • float and double literals cannot start with dot. So

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.

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

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

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

'No Dumb Expression' rule

  • no dumb expression rule, so we will catch dumb expressions (where a carriage return has broken the script). e.g.

Markup and builders

  • markup / builders will change a little, but the classic syntax still applies. Not sure of the new syntax, but we'll have some kinda start/stop syntax to denote a markup block. Maybe a keyword, like

Strings and GStrings

  • single and double quote strings can only span one line; for multiple lines use triple quotes
  • heredocs removal - they are kinda ugly anyway (smile). If you want to use them, just use treble quote instead
  • escaping of $ inside GStrings must use \$
  • A new string definition is also supported which is escaping friendly, and thus particularly friendly for regex notation:

Assertions

  • assert uses comma instead of colon to delimit the two parameter form of assertion statement

return/break/continue semantics in closures

NOT YET IMPLEMENTED

  • return/break/continue to behave inside closures like these statements work in other blocks (such as the block on a for() or while() loop. More details here

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.

Becomes

Exclusive range

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

Instead of:

The syntax is now:

  • No labels

1 Comment

  1. Hi Ed

    Thanks for your comments; we'll take them on board. The reason for the changes is to migrate to the JSR approved Groovy; which fixes some of the ambiguities in the language.

    If you don't want to move to the JSR version of Groovy, you can stay on beta-9 or the new beta-10 which will be out soon.

    Note that...

    • you generally don't need to use 'def' unless declaring typeless variables; so you can stick to the Java way if you prefer and not use 'def'.
    • we could well just make the block alignment a warning (which you could turn off) if you'd prefer - though the reason for this change was to avoid confusion with closure blocks