July 2013
Intermediate to advanced
370 pages
8h 27m
English
We can use Groovy’s support for operator overloading—judiciously—to create DSLs (see Chapter 19, Creating DSLs in Groovy). When Java has no support for operator overloading, how does Groovy get away with that? It’s really simple: each operator has a standard mapping to methods.[16] In Java we can use those methods, and on the Groovy side we can use either the operators or their corresponding methods.
Here’s an example to show operator overloading in action:
| GroovyForJavaEyes/OperatorOverloading.groovy | |
| | for(ch = 'a'; ch < 'd'; ch++) { |
| | println ch |
| | } |
We’re looping through the characters a through c using the ++ operator. This operator
maps to the
next
method on the String class to produce this output: ...
Read now
Unlock full access