Why Dynamic Languages?

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 ...

Get Programming Groovy 2 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.