July 2013
Intermediate to advanced
370 pages
8h 27m
English
Dynamic languages have the ability to extend a program at runtime, including changing types, behaviors, and object structures. With these languages, we can do things at runtime that static languages do at compile time; we can even execute program statements that are created on the fly at runtime.
For example, if we want to compute a five percent raise on an $80,000 salary, we could simply write the following:
| | 5.percentRaise(80000) |
Yes, that’s the friendly java.lang.Integer responding to our own dynamic method, which we can add quite easily, like so:
| | Integer.metaClass.percentRaise = { amount -> amount * (1 + delegate / 100.0) } |
As we see here, it’s easy to add dynamic methods to classes in Groovy. The dynamic method ...
Read now
Unlock full access