Appendix A. Groovy reference

It’s not always easy to find the information you want about Groovy, so we’ve incorporated a short reference here that includes useful information in an easily digested form. It focuses on two Groovy features: operator overloading and the extension methods that Groovy adds to the core Java class library.

A.1. Operator overloading

Groovy allows you to support operators on your own classes, such as +, *, <<, and so on. The mechanism for this is straightforward: implement methods with specific names. If you want to support adding two matrices together with the + operator, you can implement plus():

class Matrix {
    ...
    Matrix plus(Matrix other) {
        ...
    }
}

Even if you don’t implement any of the operator methods yourself, ...

Get Grails in Action, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.