DevOps / Apache Groovy Interview questions
How does Groovy implement operator overloading?
Groovy maps standard operators onto specific method names that a class can implement - for example, defining a plus(other) method on a class lets instances of it be combined with the + operator, and Groovy automatically routes a + b to a.plus(b).
This covers most common operators, minus for -, multiply for *, and so on for comparisons and other operators too, so any class can participate naturally in Groovy's operator syntax just by implementing the corresponding method, without any special operator-specific syntax to learn.
Because it's just ordinary method dispatch under a conventional name, operator overloading in Groovy follows the same dynamic, or static under @CompileStatic, dispatch rules as any other method call - there's no separate operator-specific mechanism layered on top.
More Related questions...