Another transformation is @Lazy. Sometimes, you want to handle the initialization of a field of your class lazily, so that its value is computed only on first use, often because it may be time-consuming or memory-expensive to create. The usual approach is to customize the getter of said field, so that it takes care of the initialization when the getter is called the first time. But in Groovy 1.6, you can now use the @Lazy annotation for that purpose:
In the case of complex computation for initializing the field, you may need to call some method for doing the work, instead of a value like our pets list. It is then possible to have the lazy evaluation being done by a closure call, as the following example shows:
There is also an option for leveraging Soft references for garbage collection friendliness for expensive data structures that may be contained by such lazy fields:
The internal field created by the compiler for pets will actually be a Soft reference, but accessing p.pets directly will return the value (ie. the list of pets) held by that reference, making the use of the soft reference transparent to the user of that class.