Syntax for defining enhancements
- To define an enhancement, use the keyword
enhancementinstead ofclass. - To define what to enhance, use the syntax:
: TYPETOEXTENDinstead ofextends CLASSTOEXTEND.
For example, the following enhancement adds one standard method to the basic String class and
one property:
package example
enhancement StringTestEnhancement : java.lang.String {
public function myMethod(): String {
return "Secret message!"
}
public property get myProperty() : String {
return "length : " + this.length()
}
}
With this example, use code like the following to get values:
// Get an enhancement property:
print("This is my string".myProperty)
// Get an enhancement method:
print("This is my string".myMethod())
These lines print the following:
"length: 17" "Secret message!"
Enhancements on arrays
- For regular types, use standard array syntax, such as
String[]. - For generic types, use the syntax
T[], which means all arrays.
Enhancement naming conventions
The name of your enhancement must use the convention of the enhanced type name, then an optional
functional description, and finally the word Enhancement, as shown in the following line:
[EnhancedTypeName][OptionalFunctionalDescripton]Enhancement
For example, to enhance the Report class, you could call your enhancement:
ReportEnhancement
If the enhancement adds methods related to financials data, you can the enhancement’s functional purpose by naming the enhancement:
ReportFinancialsEnhancement
Enhancement package conventions
Use your own company package to hierarchically group your own code and separate it from built-in types, in
almost all cases. For example, you could define your enhancement with the fully qualified name
com.mycompany.ReportEnhancement. Even if you are enhancing a built-in type, if at all
possible use your own package for the enhancement class itself.
In extremely rare cases, you might need to enhance a built-in type and to use a protected
property or method. If so, you might need to define your enhancement in a subpackage of the enhanced type.
However, to avoid namespace conflicts with built-in types, avoid this approach if possible.
See also
Setting properties in enhancements
You can add new properties and add property set functions to set those properties. However, in contrast to a class, enhancements cannot define new variables on the type to store instance data for your enhancement. This restriction limits most types of state management if you cannot directly change the source code for the enhanced type to add more variables to the enhanced type. Enhancements cannot add new variables because different types have different property storage techniques, such as persistent database storage, Gosu memory storage, or file-based storage. Enhancements cannot transparently mirror these storage mechanisms.
Also, although enhancements can add properties, enhancements cannot override existing properties.
Within enhancement methods, your code can set other values as appropriate such as an existing class instance
variable. You can also set properties with the property set
PROPERTYNAME() syntax. For example, this enhancement creates a
new settable property that appends an item to a list:
package doc.example
enhancement ListTestEnhancement<T> : java.util.ArrayList<T> {
public property set LastItem(item : T) {
this.add(item)
}
}
Test this enhancement in the Gosu Scratchpad with this code:
uses java.util.ArrayList
var strlist = new ArrayList<String>() {"abc", "def", "ghi", "jkl"}
print(strlist)
strlist.LastItem = "hello"
print(strlist)
This code prints:
[abc, def, ghi, jkl]
[abc, def, ghi, jkl, hello]
