Groovy 3.0: New default parser & Java-style Lambda syntax

Share
  • February 11, 2020

Apache Groovy 3.0 has arrived and it includes a host of new features that make it more flexible and integrates with newer Java features. For starters, it includes a new default parser (currently under the code-name Parrot) and support for Java-style Lambda syntax.

Groovy is a Java-syntax-compatible object-oriented language. With its familiar syntax and by working on top of the JVM, it is easy to integrate directly with any Java program. While Groovy isn’t currently as loved as younger languages that use the JVM, such as Kotlin, the 3.0 release introduces a few new changes that might shake things up.

So, is it time to bust out Groovy again in 2020?

SEE ALSO: OutOfMemoryError related JVM arguments

New Parser; meet Parrot.

The biggest highlight of v3.0.0 is the new parser, Parrot. Under the hood, it is more flexible and maintainable and includes support for additional syntax and language features. Parrot includes all the current language features, but it also adds a few new ones.

From the new parser’s README on GitHub, these new features include:

  • do-while loops; enhanced (now supporting commas) classic for loops, e.g. for(int i = 0, j = 10; i < j; i++, j--) {..})
  • lambda expressions, e.g. stream.map(e -> e + 1)
  • method references and constructor references
  • try-with-resources, AKA ARM
  • code blocks, i.e. {..}
  • Java style array initializers, e.g. new int[] {1, 2, 3}
  • default methods within interfaces
  • additional places for type annotations
  • new operators: identity operators(===!==), elvis assignment(?=), !in!instanceof
  • safe index, e.g. nullableVar?[1, 2]
  • non-static inner class instantiation, e.g. outer.new Inner()
  • runtime groovydoc, i.e. groovydoc starting with /**@; groovydoc attached to AST node as metadata

In 3.0, the new parser is enabled by default and can be disabled via a system property if necessary. With future versions of Groovy, the old parser will be deprecated and then removed.

From the repository, be sure to read the open discussion of Groovy 3 changes, written by Apache Groovy PMC Member Daniel Sun.

Source : JAXenter