Enhancements
Gosu enhancements provide a way for you to augment classes and other types with additional concrete methods and properties. For example, use enhancements to define additional utility methods on a class or interface that cannot be directly modified. You can enhance Guidewire entity types and also classes defined in either Gosu or Java. The most valuable use of enhancements is to define additional utility methods on a Java class or interface that cannot be directly modified. This feature is most useful if the source code for a class is unavailable or class is marked final and so cannot be subclassed.
Comparing enhancements and subclasses
Enhancements differ from subclasses in important ways. Enhancement methods and properties on a class are available to all objects of the enhanced class. Methods and enhancements on a subclass are available only to objects of the subtype, not to objects of the superclass. Use enhancements to add functionality that the original authors of a class did not provide.
Enhancing a class makes the new methods and properties available to all instances of that class, not merely to
subclass instances. For example, if you add an enhancement method to the String class, all
String instances in Gosu have the additional method.
- Enhancements cannot save state information by allocating new variables or properties on the enhanced type.
- Enhancements can create new methods but cannot override existing methods.
- To reference methods on the enhanced class or type, use the symbol
thisto access the enhancements. For example, to call the enhanced object’s myAction method, use the syntaxthis.myAction(). Do not use the keywordsuperin an enhancement.Note: Enhancements are defined in terms of the external interface of the enhanced type. The keywordsuperimplies a superclass rather than an interface, sosuperis inappropriate for enhancements.
Static dispatch of enhancements
Gosu enhancements are statically dispatched. Enhancement code is run at run time, but availability and the relevant code is determined at compile time. Availability of enhancement properties and methods is based on the compile-time type, not the run-time type. This behavior is different from regular properties and methods that are accessed at run time.
- Subtype/supertype distinctions might prevent access
- Suppose a method argument is declared as the type
Object. At compile time, the argument variable has type Object, even though at run time, it could be a much more specific type thanObject. If at run time, the object type isjava.util.ArrayList, the enhancement method onArrayListis unavailable. You can work around this limitation by downcasting (myObject as ArrayList) before accessing the enhancement. However, downcasting could fail at run time if the runtime type is not compatible withArrayList, so you must understand the risks of that technique. - Subtype/supertype distinctions might dispatch in unexpected ways
- If you have multiple enhancements that have the same names for properties and methods, there could be
enhancements on more than one type in the hierarchy. For example, consider enhancements of both the
java.util.Mapinterface andjava.util.HashMapwith different enhancement logic. At compile time, Gosu uses the most specific type for that programming context. If you use a variable declared to typejava.util.Map, its enhancement methods are determined at compile time, even if at run time that object has subtypejava.util.HashMap.
