Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

Operators

Most are listed here: Operator Overloading

TODO make this page a child of the User Guide
TODO the ! (not) operator; link to Groovy Truth documentation

Conditional Operators

Elvis Operator (?:)

The "Elvis operator" is a shortening of the ) which is useful for returning a 'sensible default' value if an expression resolves to false or null. A conversion of the example of the ternary operator example below would look like this:

Which translates to "if name is not null (or false) use that value, otherwise use 'Anonymous'."



Safe Navigation Operator (?.)

The Safe Navigation operator is used to avoid NullPointerException}}s. In the traditional case where you have a reference to an object you must verify that it is not null before accessing methods or properties of the object to avoid the {{NPE. To avoid this, the safe navigation operator will simply return null instead of throwing an exception, like so:

In the above example, if either user or user.address are null, streetName will be assigned null.



Ternary operator (? :)

See the section on

Error rendering macro 'link' : Link needs a name and a URL as arguments.
in the Java Tutorial. A common example:

Which means "if user.male is true, gender='male', otherwise gender='female'."



Collection-based Operators

  • Spread Operator (*.)
  • Range Operator (..)




Object- Related Operators




Regular Expression Operators

  • find (=~)
  • match (==~)
  • No labels